文本解析使用的常用方法:
S.split(sep=None):
返回的是分割时候的列表,
使用sep作为分隔符来分隔字符串
s = 'hello world'
s.split(" ")
['hello', 'world']
s.split("o")
['hell', ' w', 'rld']
s.split("a")
['hello world']
s.split("l")
['he', '', 'o wor', 'd']
S.join(interable):返回值是字符串
功能:把S添加到可迭代对象中
L = ['a','b','c']
L
['a', 'b', 'c']
"".join(L)
'ab**c'
s = 'hello'
"-".join(s)
'h-e-l-l-o'
列表推导式:
使用可迭代对象生成一个列表
是一个表达式。
语法:
[表达式 for 变量 in 可迭代对象]
[表达式 for 变量 in 可迭代对象 if 条件表达式]
说明:
1.用变量绑定从可迭代对象中取出的数据
2.变量放到表达式当中进行计算作为列表的一个元素
3.可迭代对象中没有数据取出时,列表表达式完成。
4.if可以没有,如果有if时,变量需要通过条件表达式测试真值,返回true的时候再带到表达式中计算。
5.如果,测试为False,则变量丢弃。
列表推导式的嵌套:
[表达式 for 变量1 in 可迭代对象1
for 变量2 in 可迭代对象2 if 条件表达式]
Congratulations @zhangxc! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Do not miss the last post from @steemitboard:
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @zhangxc! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit