如何從列表列表中製做平面列表?

我想知道是否有捷徑能夠從Python的列表清單中作出一個簡單的清單。 html

我能夠在for循環中執行此操做,可是也許有一些很酷的「單行代碼」? 我用reduce()嘗試過,可是出現錯誤。 app

ide

l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
reduce(lambda x, y: x.extend(y), l)

錯誤信息 函數

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'

#1樓

也能夠使用NumPy的flatoop

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

編輯11/02/2016:僅當子列表具備相同尺寸時纔可用。 性能


#2樓

彷佛與operator.add混淆了! 將兩個列表加在一塊兒時,正確的術語是concat ,而不是add。 operator.concat是您須要使用的。 測試

若是您認爲功能正常,那麼就這麼簡單: spa

>>> from functools import reduce
>>> list2d = ((1, 2, 3), (4, 5, 6), (7,), (8, 9))
>>> reduce(operator.concat, list2d)
(1, 2, 3, 4, 5, 6, 7, 8, 9)

您會看到reduce尊重序列類型,所以在提供元組時,您會獲得一個元組。 讓咱們嘗試一個列表: code

>>> list2d = [[1, 2, 3],[4, 5, 6], [7], [8, 9]]
>>> reduce(operator.concat, list2d)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

啊哈,您會獲得一個清單。 htm

性能如何:

>>> list2d = [[1, 2, 3],[4, 5, 6], [7], [8, 9]]
>>> %timeit list(itertools.chain.from_iterable(list2d))
1000000 loops, best of 3: 1.36 µs per loop

from_iterable很是快! 但這並不能與concat

>>> list2d = ((1, 2, 3),(4, 5, 6), (7,), (8, 9))
>>> %timeit reduce(operator.concat, list2d)
1000000 loops, best of 3: 492 ns per loop

#3樓

def flatten(l, a):
    for i in l:
        if isinstance(i, list):
            flatten(i, a)
        else:
            a.append(i)
    return a

print(flatten([[[1, [1,1, [3, [4,5,]]]], 2, 3], [4, 5],6], []))

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

#4樓

若是您願意放棄一點速度以得到更乾淨的外觀,則能夠使用numpy.concatenate().tolist()numpy.concatenate().ravel().tolist()

import numpy

l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] * 99

%timeit numpy.concatenate(l).ravel().tolist()
1000 loops, best of 3: 313 µs per loop

%timeit numpy.concatenate(l).tolist()
1000 loops, best of 3: 312 µs per loop

%timeit [item for sublist in l for item in sublist]
1000 loops, best of 3: 31.5 µs per loop

您能夠在docs numpy.concatenatenumpy.ravel中找到更多信息


#5樓

上面的Anil函數的一個壞功能是,它要求用戶始終手動將第二個參數指定爲空列表[] 。 相反,這應該是默認設置。 因爲Python對象的工做方式,應在函數內部而不是參數中設置這些對象。

這是一個工做功能:

def list_flatten(l, a=None):
    #check a
    if a is None:
        #initialize with empty list
        a = []

    for i in l:
        if isinstance(i, list):
            list_flatten(i, a)
        else:
            a.append(i)
    return a

測試:

In [2]: lst = [1, 2, [3], [[4]],[5,[6]]]

In [3]: lst
Out[3]: [1, 2, [3], [[4]], [5, [6]]]

In [11]: list_flatten(lst)
Out[11]: [1, 2, 3, 4, 5, 6]
相關文章
相關標籤/搜索