gpt4 book ai didi

python - 将列表中的一个字符串变成列表中的两个字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:00:08 24 4
gpt4 key购买 nike

我有一个这样的列表:

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

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

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

我该怎么做?我当然可以在两个名字之间使用那个空格(两个名字之间总是有一个空格),但是怎么做呢?

最佳答案

您可以使用 list comprehensionstr.split :

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

关于python - 将列表中的一个字符串变成列表中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26284273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com