>print output [(12.2817, 12.2817), (0, 0), (8.52, 8.5-6ren">
gpt4 book ai didi

python - 将 "list of tuples"转换为平面列表或矩阵

转载 作者:IT老高 更新时间:2023-10-28 20:24:19 24 4
gpt4 key购买 nike

使用 Sqlite,select .. from 命令会返回结果 output,它会打印:

>>print output
[(12.2817, 12.2817), (0, 0), (8.52, 8.52)]

这似乎是一个元组列表。我想将 output 转换为一个简单的列表:

[12.2817, 12.2817, 0, 0, 8.52, 8.52]

或 2x3 矩阵:

12.2817 12.2817
0 0
8.52 8.52

通过output[i][j]

读取

flatten 命令对第一个选项不起作用,我不知道第二个...

我们将不胜感激快速的解决方案,因为实际数据要大得多。

最佳答案

迄今为止发布的最快(和最短)的解决方案:

list(sum(output, ()))

itertools 解决方案快约 50%,比 map 解决方案快约 70%。

关于python - 将 "list of tuples"转换为平面列表或矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10632839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com