如何将多个列表中相同索引位置的字符串合并生成新列表
不同列表相同索引位置字符串拼接实现方案
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
相关产品推荐
相关产品推荐

