gpt4 book ai didi

Python - 连接 2 个列表

转载 作者:太空狗 更新时间:2023-10-29 20:39:39 26 4
gpt4 key购买 nike

大家好,我是 Python 和这个论坛的新手。

我的问题:

我有两个列表:

list_a = ['john','peter','paul']
list_b = [ 'walker','smith','anderson']

我使用 zip 成功创建了这样的列表:

list_c = zip(list_a, list_b)
print list_c
# [ 'john','walker','peter','smith','paul','anderson']

但我要查找的结果是这样的列表:

list_d = ['john walker','peter smith','paul anderson']

无论我尝试过什么,我都没有成功!我怎样才能得到这个结果?

最佳答案

您将从两个列表中获取压缩名称,只需像这样连接每一对

print map(" ".join, zip(list_a, list_b))
# ['john walker', 'peter smith', 'paul anderson']

关于Python - 连接 2 个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22642261/

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