以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。


如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?

在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。


当前回答

注意: 下面适用于 Python 3.3+ 因为它使用 yield_from. six 也是第三方包,尽管它是稳定的。


在obj = [1, 2,], [3, 4], [5, 6]的情况下,这里的所有解决方案都很好,包括列表理解和 itertools.chain.from_iterable。

但是,考虑这个稍微复杂的案例:

>>> obj = [[1, 2, 3], [4, 5], 6, 'abc', [7], [8, [9, 10]]]

这里有几个问题:

您可以以以下方式解决此问题:

>>> from collections import Iterable
>>> from six import string_types

>>> def flatten(obj):
...     for i in obj:
...         if isinstance(i, Iterable) and not isinstance(i, string_types):
...             yield from flatten(i)
...         else:
...             yield i


>>> list(flatten(obj))
[1, 2, 3, 4, 5, 6, 'abc', 7, 8, 9, 10]

在这里,您可以检查(一)的子元素(一)与(一)的Iterable(一)无效,从(一)的ABC,但也希望确保(二)的元素(一)不是“类似于(一)的”。

其他回答

这可能不是最有效的方式,但我认为要放一个单线(实际上是一个双线)。两种版本都会在任意的序列列列表上工作,并利用语言功能(Python 3.5)和回归。

def make_list_flat (l):
    flist = []
    flist.extend ([l]) if (type (l) is not list) else [flist.extend (make_list_flat (e)) for e in l]
    return flist

a = [[1, 2], [[[[3, 4, 5], 6]]], 7, [8, [9, [10, 11], 12, [13, 14, [15, [[16, 17], 18]]]]]]
flist = make_list_flat(a)
print (flist)

产量是

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

它首先以深度工作. 旋转会下降,直到它找到一个非列表元素,然后延伸当地变量板,然后转向父母。 每当板回来时,它在列表理解中延伸到父母的板。

上面的一个创建了几个地方列表,并返回它们,这些列表被用来扩展父母的列表,我认为这一点的路径可能是创建一个可怕的板块,如下。

a = [[1, 2], [[[[3, 4, 5], 6]]], 7, [8, [9, [10, 11], 12, [13, 14, [15, [[16, 17], 18]]]]]]
flist = []
def make_list_flat (l):
    flist.extend ([l]) if (type (l) is not list) else [make_list_flat (e) for e in l]

make_list_flat(a)
print (flist)

产量再次

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

虽然我目前对效率不确定。

你的功能不起作用的原因是因为延伸延伸一个序列在现场,并且不会返回它。

reduce(lambda x,y: x.extend(y) or x, l)

注意:扩展比 + 列表更有效。

matplotlib.cbook.flatten() 将为粘贴列表工作,即使它们比示例更深地粘贴。

import matplotlib
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
print(list(matplotlib.cbook.flatten(l)))
l2 = [[1, 2, 3], [4, 5, 6], [7], [8, [9, 10, [11, 12, [13]]]]]
print(list(matplotlib.cbook.flatten(l2)))

结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

这比 underscore 快 18 倍。

Average time over 1000 trials of matplotlib.cbook.flatten: 2.55e-05 sec
Average time over 1000 trials of underscore._.flatten: 4.63e-04 sec
(time for underscore._)/(time for matplotlib.cbook) = 18.1233394636

您也可以使用NumPy的公寓:

import numpy as np
list(np.array(l).flat)

它只有在超级列表具有相同的尺寸时才有效。

考虑到列表L的列表,

flat_list = [item for sublist in l for item in sublist]

意思是:

flat_list = []
for sublist in l:
    for item in sublist:
        flat_list.append(item)

它比迄今为止发布的短篇文章更快(l 是表格的列表)。

下面是相应的功能:

def flatten(l):
    return [item for sublist in l for item in sublist]

作为证据,您可以在标准图书馆中使用时间模块:

$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,l)'
1000 loops, best of 3: 1.1 msec per loop

解释:基于 + 的缩写(包括在总中使用)是必然的 O(L**2)当有 L 列表时 - 因为中间结果列表保持长,每个步骤都会分配一个新的中间结果列表对象,前中间结果中的所有对象都必须复制(以及在结尾添加一些新的对象)。

列表理解只产生一个列表,一次,并复制每个项目(从其原始居住地到结果列表)也准确一次。