如何将元组列表展平为pythonic列表

Dav*_*542 5 python list flatten

给出以下元组列表:

INPUT = [(1,2),(1,),(1,2,3)]
Run Code Online (Sandbox Code Playgroud)

我怎么把它压成一个列表?

OUTPUT ==> [1,2,1,1,2,3]
Run Code Online (Sandbox Code Playgroud)

上面有一个单行班吗?

类似:在Python中展平元组列表

iCo*_*dez 10

你可以使用列表理解:

>>> INPUT = [(1,2),(1,),(1,2,3)]
>>> [y for x in INPUT for y in x]
[1, 2, 1, 1, 2, 3]
>>>
Run Code Online (Sandbox Code Playgroud)

itertools.chain.from_iterable 在这样的情况下也会使用很多:

>>> from itertools import chain
>>> INPUT = [(1,2),(1,),(1,2,3)]
>>> list(chain.from_iterable(INPUT))
[1, 2, 1, 1, 2, 3]
>>>
Run Code Online (Sandbox Code Playgroud)

尽管如此,这并不是一个单线.