如何使用列表推导将元组元组转换为一维列表?

fro*_*die 30 python tuples list-comprehension iterable-unpacking

我有一个元组元组 - 例如:

tupleOfTuples = ((1, 2), (3, 4), (5,))
Run Code Online (Sandbox Code Playgroud)

我想按顺序将其转换为所有元素的平面一维列表:

[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

我一直试图通过列表理解来实现这一目标.但我似乎无法弄明白.我能够通过for-each循环完成它:

myList = []
for tuple in tupleOfTuples:
   myList = myList + list(tuple)
Run Code Online (Sandbox Code Playgroud)

但我觉得必须有一种方法可以通过列表理解来做到这一点.

一个简单的[list(tuple) for tuple in tupleOfTuples]只是给你一个列表列表,而不是单个元素.我想我可以通过使用解包操作符然后解压缩列表来构建它,如下所示:

[*list(tuple) for tuple in tupleOfTuples]
Run Code Online (Sandbox Code Playgroud)

要么

[*(list(tuple)) for tuple in tupleOfTuples]
Run Code Online (Sandbox Code Playgroud)

......但那没用.有任何想法吗?或者我应该坚持循环?

Sil*_*ost 62

它通常被称为展平嵌套结构.

>>> tupleOfTuples = ((1, 2), (3, 4), (5,))
>>> [element for tupl in tupleOfTuples for element in tupl]
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

只是为了证明效率:

>>> import timeit
>>> it = lambda: list(chain(*tupleOfTuples))
>>> timeit.timeit(it)
2.1475738355700913
>>> lc = lambda: [element for tupl in tupleOfTuples for element in tupl]
>>> timeit.timeit(lc)
1.5745135182887857
Run Code Online (Sandbox Code Playgroud)

ETA:请不要使用tuple变量名称,它内置阴影.

  • 努力理解它是如何工作的。您可以推荐任何资源并提供逐步说明吗? (2认同)

ken*_*ytm 39

sum如果你没有很多元组就可以使用.

>>> tupleOfTuples = ((1, 2), (3, 4), (5,))
>>> sum(tupleOfTuples, ())
(1, 2, 3, 4, 5)
>>> list(sum(tupleOfTuples, ())) # if you really need a list
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

如果你确实有很多元组,请使用列表推导chain.from_iterable防止二次方行为sum.


微基准测试:

  • Python 2.6

    • 长元组的短元组

      $ python2.6 -m timeit -s 'tot = ((1, 2), )*500' '[element for tupl in tot for element in tupl]'
      10000 loops, best of 3: 134 usec per loop
      $ python2.6 -m timeit -s 'tot = ((1, 2), )*500' 'list(sum(tot, ()))'
      1000 loops, best of 3: 1.1 msec per loop
      $ python2.6 -m timeit -s 'tot = ((1, 2), )*500; from itertools import chain; ci = chain.from_iterable' 'list(ci(tot))'
      10000 loops, best of 3: 60.1 usec per loop
      $ python2.6 -m timeit -s 'tot = ((1, 2), )*500; from itertools import chain' 'list(chain(*tot))'
      10000 loops, best of 3: 64.8 usec per loop
      
      Run Code Online (Sandbox Code Playgroud)
    • 长元组的短元组

      $ python2.6 -m timeit -s 'tot = ((1, )*500, (2, )*500)' '[element for tupl in tot for element in tupl]'
      10000 loops, best of 3: 65.6 usec per loop
      $ python2.6 -m timeit -s 'tot = ((1, )*500, (2, )*500)' 'list(sum(tot, ()))'
      100000 loops, best of 3: 16.9 usec per loop
      $ python2.6 -m timeit -s 'tot = ((1, )*500, (2, )*500); from itertools import chain; ci = chain.from_iterable' 'list(ci(tot))'
      10000 loops, best of 3: 25.8 usec per loop
      $ python2.6 -m timeit -s 'tot = ((1, )*500, (2, )*500); from itertools import chain' 'list(chain(*tot))'
      10000 loops, best of 3: 26.5 usec per loop
      
      Run Code Online (Sandbox Code Playgroud)
  • Python 3.1

    • 长元组的短元组

      $ python3.1 -m timeit -s 'tot = ((1, 2), )*500' '[element for tupl in tot for element in tupl]'
      10000 loops, best of 3: 121 usec per loop
      $ python3.1 -m timeit -s 'tot = ((1, 2), )*500' 'list(sum(tot, ()))'
      1000 loops, best of 3: 1.09 msec per loop
      $ python3.1 -m timeit -s 'tot = ((1, 2), )*500; from itertools import chain; ci = chain.from_iterable' 'list(ci(tot))'
      10000 loops, best of 3: 59.5 usec per loop
      $ python3.1 -m timeit -s 'tot = ((1, 2), )*500; from itertools import chain' 'list(chain(*tot))'
      10000 loops, best of 3: 63.2 usec per loop
      
      Run Code Online (Sandbox Code Playgroud)
    • 长元组的短元组

      $ python3.1 -m timeit -s 'tot = ((1, )*500, (2, )*500)' '[element for tupl in tot for element in tupl]'
      10000 loops, best of 3: 66.1 usec per loop
      $ python3.1 -m timeit -s 'tot = ((1, )*500, (2, )*500)' 'list(sum(tot, ()))'
      100000 loops, best of 3: 16.3 usec per loop
      $ python3.1 -m timeit -s 'tot = ((1, )*500, (2, )*500); from itertools import chain; ci = chain.from_iterable' 'list(ci(tot))'
      10000 loops, best of 3: 25.4 usec per loop
      $ python3.1 -m timeit -s 'tot = ((1, )*500, (2, )*500); from itertools import chain' 'list(chain(*tot))'
      10000 loops, best of 3: 25.6 usec per loop
      
      Run Code Online (Sandbox Code Playgroud)

观察:

  • sum 如果外部元组很短,则速度更快.
  • list(chain.from_iterable(x)) 如果外部元组很长,则速度更快.


Joc*_*zel 10

你把元组连在一起:

from itertools import chain
print list(chain(*listOfTuples))
Run Code Online (Sandbox Code Playgroud)

如果你熟悉的话,应该是非常易读的itertools,没有明确的list你甚至可以用生成器形式得到你的结果.


Don*_*ner 9

我喜欢在这种情况下使用'reduce'(这是减少的!)

lot = ((1, 2), (3, 4), (5,))
print list(reduce(lambda t1, t2: t1 + t2, lot))

 > [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)

  • 在亚历克斯用他对地图的仇恨/减少愤怒投票给你之前 (2认同)

Cra*_*der 9

这些答案中的大多数仅适用于单一的扁平化.有关更全面的解决方案,请尝试此操作(来自http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html):

def flatten(l, ltypes=(list, tuple)):
    ltype = type(l)
    l = list(l)
    i = 0
    while i < len(l):
        while isinstance(l[i], ltypes):
            if not l[i]:
                l.pop(i)
                i -= 1
                break
            else:
                l[i:i + 1] = l[i]
        i += 1
    return ltype(l)
Run Code Online (Sandbox Code Playgroud)