转换“元组列表"进入平面列表或矩阵 [英] Transform "list of tuples" into a flat list or a matrix
问题描述
使用Sqlite,"select..from"命令返回结果输出",该结果打印(在python中):
With Sqlite, a "select..from" command returns the results "output", which prints (in python):
>>print output
[(12.2817, 12.2817), (0, 0), (8.52, 8.52)]
这似乎是一个元组列表.我想将输出"转换为简单的1D数组(我猜是Python中的列表):
It seems to be a list of tuples. I would like to either convert "output" in a simple 1D array (=list in Python I guess):
[12.2817, 12.2817, 0, 0, 8.52, 8.52]
或2x3矩阵:
12.2817 12.2817
0 0
8.52 8.52
通过"output [i] [j]"读取
to be read via "output[i][j]"
flatten命令不执行第一个选项的工作,而我不知道第二个选项...:)
The flatten command does not do the job for the 1st option, and I have no idea for the second one... :)
能给我一个提示吗?快速的事情会变得很棒,因为实际数据会更大(这只是一个简单的示例).
Could you please give me a hint? Some thing fast would be great as real data are much bigger (here is just a simple example).
推荐答案
到目前为止,发布的最快(也是最短)解决方案是:
By far the fastest (and shortest) solution posted:
list(sum(output, ()))
比itertools
解决方案快约50%,比map
解决方案快约70%.
About 50% faster than the itertools
solution, and about 70% faster than the map
solution.
这篇关于转换“元组列表"进入平面列表或矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!