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

Python如何将两个列表按对应位置拼接元素得到新列表

实现方法

你说的没错,单独使用append()、extend()、insert()确实无法直接完成两个列表同位置元素的拼接需求,你可以通过以下几种方式实现:

方法1:列表推导式(最推荐,写法简洁)

通过zip()函数配对两个列表同位置的元素,遍历后直接拼接:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
result = [item1 + str(item2) for item1, item2 in zip(list1, list2)]

运行后result的输出就是["a1", "b2", "c3"]

方法2:索引遍历(逻辑最直观)

如果对列表推导式不熟悉,也可以通过索引遍历两个列表的对应元素拼接:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
result = []
for i in range(len(list1)):
    result.append(list1[i] + str(list2[i]))

方法3:map函数实现

也可以用map()函数批量处理配对元素:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
result = list(map(lambda x, y: x + str(y), list1, list2))

注意:以上方法都要求两个列表长度一致,zip()默认会以较短的列表长度为准,超出长度的元素会被忽略。


内容的提问来源于stack exchange,提问作者James Xu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 11:51:02