python-3.x - 有没有办法解压缩嵌套冗余列表的列表?
问题描述
我有这个清单:
input = [[[1,2]], [[3,4]], [[5,6]]]
想要的输出:
output = [[1,3,5],[2,4,6]]
我试过这个:
x, y = map(list,zip(*input))
后来意识到这种方法由于多余的方括号而不起作用,有没有一种方法可以在没有迭代的情况下解决这个问题。
解决方案
In [117]: input = [[[1,2]], [[3,4]], [[5,6]]]
In [118]: list(zip(*[i[0] for i in input]))
Out[118]: [(1, 3, 5), (2, 4, 6)]
In [119]: list(map(list, zip(*[i[0] for i in input])))
Out[119]: [[1, 3, 5], [2, 4, 6]]
推荐阅读
- java - 在 Room 数据库中保存数据时出错,例如图像、电子邮件和用户名
- ssl - 使用 Telit 模块启用 SSL
- python - 如何在 Telethon 中使用 InputPhoto?
- scala - 使用从类型枚举中选择的泛型
- sap-cloud-sdk - 无法使用 Cloud SDK 库启动调用 Workflow Rest API 的应用程序
- python-3.7 - 在 Yocto 中完全移除 Python2 及相关组件
- excel - Excel“IF”公式:计算经纪费用
- postgresql - Postgresql:是否可以仅复制指定的列而不将空值插入其他列
- sql - BigQuery/GDELT 的奇怪结果。如何正确解析日期?
- java - 如何通过 Java 使用 ssl 和证书向 ldap (AD) 拨打电话?