我有一个清单:
l = ['UIS006538, UIS036621']
我想将其转换为
l = ['UIS006538', 'UIS036621']
我该怎么做?
答案 0 :(得分:2)
使用str.split
分割字符串项:
>>> l = ['UIS006538, UIS036621']
>>> l[0].split(', ')
['UIS006538', 'UIS036621']
如果列表中有多个项目,请使用list comprehension:
>>> l = ['UIS006538, UIS036621', 'a, b, c']
>>> [x.split(', ') for x in l]
[['UIS006538', 'UIS036621'], ['a', 'b', 'c']]
如果你想要平面列表:
>>> [x for xs in l for x in xs.split(', ')]
['UIS006538', 'UIS036621', 'a', 'b', 'c']