我有一个列表:
['Mark', 'Reynold', 'Peter', 'Randall Macenroe'] #The list is a lot longer, so I can't go by index
我想将该列表更改为另一个列表:
['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']
我该怎么做?我确定可以在两个名称之间使用该空格(两个名称之间总会有空格),但是如何?
答案 0 :(得分:5)
您可以使用list comprehension和str.split
:
>>> lst = ['Mark', 'Reynold', 'Peter', 'Randall Macenroe']
>>> [y for x in lst for y in x.split()]
['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']
>>>