列出项目类型转换

时间:2014-09-14 04:42:21

标签: python string list

我有一个清单:

l = ['UIS006538, UIS036621']

我想将其转换为

l = ['UIS006538', 'UIS036621']

我该怎么做?

1 个答案:

答案 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']