>>> industries_list = ["Computers, Internet","Photography, Tourism","Motoring, Manufacturing"]
>>> [s.split(',') for s in industries_list]
[['Computers', ' Internet'], ['Photography', ' Tourism'], ['Motoring', ' Manufacturing']]
删除空白:
^{pr2}$
您还可以使用纯列表理解(嵌入式列表理解):
>>> [[w.strip() for w in s.split(',')] for s in industries_list]
[['Computers', 'Internet'], ['Photography', 'Tourism'], ['Motoring', 'Manufacturing']]
industries_list = [s.split(',') for s in industries_list]
您可能需要去掉结果周围的多余空格:
^{pr2}$
演示:
>>> industries_list = ["Computers, Internet","Photography, Tourism","Motoring, Manufacturing"]
>>> [s.split(',') for s in industries_list]
[['Computers', ' Internet'], ['Photography', ' Tourism'], ['Motoring', ' Manufacturing']]
>>> [[w.strip() for w in s.split(',')] for s in industries_list]
[['Computers', 'Internet'], ['Photography', 'Tourism'], ['Motoring', 'Manufacturing']]
使用列表理解:
删除空白:
^{pr2}$您还可以使用纯列表理解(嵌入式列表理解):
在列表理解中按
','
拆分每个值:您可能需要去掉结果周围的多余空格:
^{pr2}$演示:
在string类上使用
.split
:如果您不想占用空间:
^{pr2}$Live demo.
相关问题 更多 >
编程相关推荐