You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何将两个列表的对应元素拼接生成新列表

Python 两个等长列表对应位置元素拼接实现方案

以下是你的场景适用的几种实现方式,优先推荐第一种最简方案:

方法1:列表推导式 + zip(最推荐)

zip() 会自动将两个列表对应位置的元素配对成元组,配合f-string直接拼接即可:

Names = ["John", "Marc", "Alex"]
Surnames = ["Stuart", "Smith", "Jones"]

result = [f"{name}-{surname}" for name, surname in zip(Names, Surnames)]
print(result)
# 输出:['John-Stuart', 'Marc-Smith', 'Alex-Jones']

如果需要避免两个列表长度不一致时漏报错,Python 3.10+ 版本可以给zip加strict=True参数,长度不一致时会直接抛出异常,避免得到不符合预期的结果:

result = [f"{name}-{surname}" for name, surname in zip(Names, Surnames, strict=True)]

方法2:map 函数实现

也可以用map配合匿名函数完成拼接:

result = list(map(lambda name, surname: f"{name}-{surname}", Names, Surnames))

方法3:索引遍历(仅做逻辑参考,不推荐日常使用)

如果是刚接触Python想理解底层逻辑,可以用索引遍历的方式实现,不过代码冗余度更高,性能也不如前两种方案:

result = []
for i in range(len(Names)):
    result.append(f"{Names[i]}-{Surnames[i]}")

内容的提问来源于stack exchange,提问作者Alejandro Ueno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 18:45:11