2024-10-01 07:45:03 发布
网友
我有这样一个清单:
['Mark', 'Reynold', 'Peter', 'Randall Macenroe'] #The list is a lot longer, so I can't go by index
我想把这个列表换成另一个列表:
['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']
我该怎么做?我当然可以用两个名字之间的空格(两个名字之间总是有空格),但是怎么用呢
可以使用list comprehension和^{}:
>>> lst = ['Mark', 'Reynold', 'Peter', 'Randall Macenroe'] >>> [y for x in lst for y in x.split()] ['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe'] >>>
可以使用list comprehension和^{} :
相关问题 更多 >
编程相关推荐