我有一个列表看起来像这样:
[[1,2,3],[1,2],[1,4,5,6,7]]
我想把它弄平 [1,2,3,1,2,1,4,5,6,7]
[1,2,3,1,2,1,4,5,6,7]
有没有使用numpy的轻量级功能来执行此操作?
如果没有numpy(ndarray.flatten),一种使用方式 chain.from_iterable是itertools.chain:
ndarray.flatten
chain.from_iterable
itertools.chain
>>> list(chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]])) [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]
或者作为另一种Python方式,您可以使用 列表理解 :
[j for sub in [[1,2,3],[1,2],[1,4,5,6,7]] for j in sub]
另一个非常适合短列表的功能方法也可以reduce在Python2和functools.reducePython3中使用(不要将其用于长列表):
reduce
functools.reduce
In [4]: from functools import reduce # Python3 In [5]: reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]]) Out[5]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]
为了使其更快一点,您可以使用operator.add内置,而不是lambda:
operator.add
lambda
In [6]: from operator import add In [7]: reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]]) Out[7]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7] In [8]: %timeit reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]]) 789 ns ± 7.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) In [9]: %timeit reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]]) 635 ns ± 4.38 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
基准:
:~$ python -m timeit "from itertools import chain;chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]])" 1000000 loops, best of 3: 1.58 usec per loop :~$ python -m timeit "reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])" 1000000 loops, best of 3: 0.791 usec per loop :~$ python -m timeit "[j for i in [[1,2,3],[1,2],[1,4,5,6,7]] for j in i]" 1000000 loops, best of 3: 0.784 usec per loop
使用@Will答案的基准测试sum(对于短列表而言,它的速度很快,但对于长列表而言,它的速度很快):
sum
:~$ python -m timeit "sum([[1,2,3],[4,5,6],[7,8,9]], [])" 1000000 loops, best of 3: 0.575 usec per loop :~$ python -m timeit "sum([range(100),range(100)], [])" 100000 loops, best of 3: 2.27 usec per loop :~$ python -m timeit "reduce(lambda x,y :x+y ,[range(100),range(100)])" 100000 loops, best of 3: 2.1 usec per loop