python - 我想在python中组合一个元组列表
问题描述
我有一个 x 和 y 坐标的元组列表,例如: [(1,2), (3,4), (5,6)] 我想操纵该列表成为一个 2xN 矩阵(取决于有多少数字),这样我就有了一个只有 x 坐标和 y 坐标的列表。基本上我想通过一个函数输出: [(1,3,5), (2,4,6)] ,但不完全确定该怎么做。
解决方案
list(zip(*[(1, 2), (3, 4), (5, 6)]))
将输出:
[(1, 3, 5), (2, 4, 6)]
>>> a = [(1, 2), (3, 4), (5, 6)]
>>> zip(*a) # Returns a zipped object.
<zip object at 0x10c9f5540>
>>> print(*a) # "Star a" unpacks the list.
(1, 2) (3, 4) (5, 6)
>>> print(a[0], a[1], a[2]) # Above is equivalent to:
(1, 2) (3, 4) (5, 6)
>>> b, c = [1, 2], [3, 4]
>>> list(zip(b, c))
[(1, 3), (2, 4)] # Zips the inputs of both lists in the pattern (b1, c1) and (b2, c2). list() converts the "zip" generator to a list.
>>> list(zip(*a))
[(1, 3, 5), (2, 4, 6)] # Zips the wanted ints with the logic above.
推荐阅读
- data-visualization - 在桌面桌面上打开工作簿时出现奇怪的错误
- javascript - 如何使用该文档中的另一个变量更改文档中变量的值?
- arrays - 如何使用 mongoose 将数组中的每个项目保存为文档?如果任何文档与数组中的项目匹配,则不应创建它
- python - PyMySQL 查询字符串生成器
- c# - 仅使用字符串导航对象获取嵌套对象中的属性值
- android - 第一次在 Android 上启动 Unity 应用时,相机显示黑色
- git - 使用快速导出将 mercurial repo 转换为 git 时至少出现一个未命名的头部错误
- javascript - 我如何将金额输入字段设置为不可编辑和可编辑我正在使用 angularJS 编程语言
- android - 管理图像层次结构的最佳方法是什么?
- vb.net - Backgroundworker 给出多个错误信息