简体   繁体   English

在Python中将列表列表转换为元组

[英]Converting a list of lists to a tuple in Python

I have a list of lists (generated with a simple list comprehension): 我有一个列表列表(使用简单的列表推导生成):

>>> base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]
>>> base_lists

[[1,1],[1,2],[1,3],[1,4],[1,5],[2,1],[2,2],[2,3],[2,4],[2,5]]

I want to turn this entire list into a tuple containing all of the values in the lists, ie: 我想将整个列表转换为包含列表中所有值的元组,即:

resulting_tuple = (1,1,1,2,1,3,1,4,1,5,2,1,2,2,2,3,2,4,2,5)

What would the most effective way to do this be? 最有效的方法是什么? (A way to generate this same tuple with list comprehension would also be an acceptable answer.) I've looked at answers here and in the Python documentation, however I have been unable to find a suitable one. (使用列表理解生成这个相同元组的方法也是可以接受的答案。)我在这里和Python文档中查看了答案,但是我找不到合适的答案。

EDIT: 编辑:

Many thanks to all who answered! 非常感谢所有回答的人!

tuple(x for sublist in base_lists for x in sublist)

Edit : note that, with base_lists so short, the genexp (with unlimited memory available) is slow. 编辑 :请注意,如果base_lists如此之短,则genexp(可用内存不受限制)很慢。 Consider the following file tu.py : 考虑以下文件tu.py

base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]

def genexp():
  return tuple(x for sublist in base_lists for x in sublist)

def listcomp():
  return tuple([x for sublist in base_lists for x in sublist])

def withsum():
  return tuple(sum(base_lists,[]))

import itertools as it

def withit():
  return tuple(it.chain(*base_lists))

Now: 现在:

$ python -mtimeit -s'import tu' 'tu.genexp()'
100000 loops, best of 3: 7.86 usec per loop
$ python -mtimeit -s'import tu' 'tu.withsum()'
100000 loops, best of 3: 5.79 usec per loop
$ python -mtimeit -s'import tu' 'tu.withit()'
100000 loops, best of 3: 5.17 usec per loop
$ python -mtimeit -s'import tu' 'tu.listcomp()'
100000 loops, best of 3: 5.33 usec per loop

When lists are longer (ie, when performance really matters) things are a bit different. 当列表更长时(即,当性能真的很重要时),事情会有所不同。 Eg, putting a 100 * on the RHS defining base_lists : 例如,在RHS上定义一个100 *定义base_lists

$ python -mtimeit -s'import tu' 'tu.genexp()'
1000 loops, best of 3: 408 usec per loop
$ python -mtimeit -s'import tu' 'tu.withsum()'
100 loops, best of 3: 5.07 msec per loop
$ python -mtimeit -s'import tu' 'tu.withit()'
10000 loops, best of 3: 148 usec per loop
$ python -mtimeit -s'import tu' 'tu.listcomp()'
1000 loops, best of 3: 278 usec per loop

so for long lists only withsum is a performance disaster -- the others are in the same ballpark, although clearly itertools has the edge, and list comprehensions (when abundant memory is available, as it always will be in microbenchmarks;-) are faster than genexps. 所以对于长列表只有withsum是一个性能灾难 - 其他人在同一个球场,虽然显然itertools有优势,列表理解(当丰富的内存可用时,因为它总是在微基准测试中;-)比genexps。

Using 1000 * , genexp slows down by about 10 times (wrt the 100 * ), withit and listcomp by about 12 times, and withsum by about 180 times (withsum is O(N squared) , plus it's starting to suffer from serious heap fragmentation at that size). 使用1000 * ,genexp减慢大约10倍(与100 * ),withit和listcomp大约减少12次,并且减少大约180次(withsum是O(N squared) ,加上它开始遭受严重的堆碎片化在那个大小)。

from itertools import chain
base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]

print tuple(chain(*base_lists))
>>> sum(base_lists,[])
[1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5]
>>> tuple(sum(base_lists,[]))
(1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5)

resulting_tuple = tuple(item for l in base_lists for item in l)

>>> arr=[]
>>> base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]
>>> [ arr.extend(i) for i in base_lists ]
[None, None, None, None, None, None, None, None, None, None]
>>> arr
[1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5]
>>> tuple(arr)
(1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM