2016-08-23 102 views

回答

2

你只需要split每個字符串:

result = [s.split(',') for s in cat] 
1

試試這個:

cat = ['a','a,b,c','c,d,e,f'] 
newlist = [a.split(',') for a in cat] 
print(newlist) 
>>> [['a'], ['a', 'b', 'c'], ['c', 'd', 'e', 'f']] 
4

您可以通過list comprehension實現這一目標爲:

>>> cat = ['a','a,b,c','c,d,e,f'] 
>>> [c.split(',') for c in cat] 
[['a'], ['a', 'b', 'c'], ['c', 'd', 'e', 'f']] 

或者,你也可以使用lambda功能與map來實現這一目標:

>>> map(lambda x: x.split(','), cat) 
[['a'], ['a', 'b', 'c'], ['c', 'd', 'e', 'f']] 
相關問題