![](/img/trans.png)
[英]How to combine two lists to make each element of the list have two values, one from each list?
[英]How to combine elements in two lists into one element in one list?
我的问题是我尝试合并 2 个列表:
alpha = ['red','white','blue']
beta = ['shirt','car','house']
进入:
delta = ['red shirt','white car','blue house']
我尝试使用zip()
,但它返回: [('red', 'shirt'), ('white', 'car'), ('blue', 'house')]
这不是我寻找。
我该怎么做? 先感谢您。
您只需要进一步 go 并创建一个联合字符串
[' '.join(group) for group in zip(alpha, beta)]
您可以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))])
给我:
['red shirt', 'white car', 'blue house']
编辑:实际上其他人发布了比我更好的解决方案:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.