python - 压缩嵌套列表

标签 python list tuples

我正在尝试但无法以特定方式压缩以下两个列表:

list1 = [(1,2,3),(4,5,6),(7,8,9)]
list2 = [10,11,12]
zippedlist = [(1,2,3,10),(4,5,6,11),(7,8,9,12)]

我最初认为解压缩 list1 并运行 zip(*list1,list2) 可以完成这项工作,但我现在明白这是行不通的。

我怀疑这可以使用带有 zip 函数的一个或多个 for 循环来完成,但我不太确定它是如何工作的。关于我如何继续的任何建议?

最佳答案

您还可以使用 map :

list(map(lambda x, y: x +(y,), list1, list2))
# [(1, 2, 3, 10), (4, 5, 6, 11), (7, 8, 9, 12)]

关于python - 压缩嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946371/

相关文章:

python - 在大量列中更改数据框中特定列的位置(索引)?

python - Gzip 到 base64 编码将字符添加到 JSON 字符串

python - 将方程解析为 Python 中的元组列表

python - 为什么我的解析器无法在不崩溃的情况下将 Sprite 绘制到屏幕上?

python - 在 python 中格式化对象列表

Python:连接、字符串、迭代和 DWIM

python - 将几个变量 append 到 Python 中的列表

c++ - 将元组的内容作为可变函数参数传递

python - Pandas 元组 groupby 聚合

python - 如何根据记录中其他 4 个字段的 bool 运算符有效更新数据框中的字段?