将列表中的字符串设置为列表中的两个字符串

时间:2014-10-09 17:16:05

标签: python list python-2.7 split

我有一个列表:

['Mark', 'Reynold', 'Peter', 'Randall Macenroe'] #The list is a lot longer, so I can't go by index

我想将该列表更改为另一个列表:

['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']

我该怎么做?我确定可以在两个名称之间使用该空格(两个名称之间总会有空格),但是如何?

1 个答案:

答案 0 :(得分:5)

您可以使用list comprehensionstr.split

>>> lst = ['Mark', 'Reynold', 'Peter', 'Randall Macenroe']
>>> [y for x in lst for y in x.split()]
['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']
>>>