[英]How to combine elements in two lists into one element in one list?
My problem is that I try to combine 2 lists:我的问题是我尝试合并 2 个列表:
alpha = ['red','white','blue']
beta = ['shirt','car','house']
into:进入:
delta = ['red shirt','white car','blue house']
I tried to use zip()
, but it returns: [('red', 'shirt'), ('white', 'car'), ('blue', 'house')]
which is not what I'm looking for.我尝试使用zip()
,但它返回: [('red', 'shirt'), ('white', 'car'), ('blue', 'house')]
这不是我寻找。
How can I do it?我该怎么做? Thank you in advance.先感谢您。
You just need to go one step further and create a joint string您只需要进一步 go 并创建一个联合字符串
[' '.join(group) for group in zip(alpha, beta)]
You can map
with join
over the lists zipped together:您可以map
与压缩join
的列表连接:
list(map(' '.join, zip(alpha, beta)))
# ['red shirt', 'white car', 'blue house']
print([alpha[i]+" "+beta[i] for i in range(len(alpha))])
Gives me:给我:
['red shirt', 'white car', 'blue house']
Edit: actually others have posted better solutions than mine:-)编辑:实际上其他人发布了比我更好的解决方案:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.