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

如何将多个列表中相同索引位置的字符串合并生成新列表

不同列表相同索引位置字符串拼接实现方案

Python 实现

方法1:zip + 列表推导式(最简洁写法)

a = ["Hello", "Hi"]
b = ["World", "friends"]
result = [x + y for x, y in zip(a, b)]
# 输出结果:['HelloWorld', 'Hifriends']
  • 规则说明:如果两个列表长度不一致,zip()会默认匹配到较短列表的最后一位为止,长列表超出的元素会自动忽略。
  • 适配不等长列表的写法(缺失位置补空字符串):
from itertools import zip_longest
result = [x + y for x, y in zip_longest(a, b, fillvalue="")]

方法2:索引遍历(适合自定义处理逻辑场景)

a = ["Hello", "Hi"]
b = ["World", "friends"]
result = []
max_index = min(len(a), len(b))
for i in range(max_index):
    result.append(a[i] + b[i])

JavaScript 实现

const a = ["Hello", "Hi"];
const b = ["World", "friends"];
const result = a.map((item, index) => item + b[index]);
// 输出结果:['HelloWorld', 'Hifriends']

内容的提问来源于stack exchange,提问作者Aadhi Arun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 16:24:02