问题描述
我有这样的python代码:
I have python code like this:
newlist =[[52, None, None], [129, None, None], [56, None, None], [111, None, None],
[22, None, None], [33, None, None], [28, None, None], [52, None, None],
[52, None, None], [52, None, None], [129, None, None], [56, None, None],
[111, None, None], [22, None, None], [33, None, None], [28, None, None]]
我想要 newlist
像:
newlist =[52, None, None,129, None, None,56, None, None,111, None, None,22,
None, None,33, None, None,28, None, None,52, None, None,52, None,
None,52, None, None,129, None, None,56, None, None, 111, None,
None,22, None, None,33, None, None,28, None, None]
有什么办法可以解决吗?
Is there any way to work around ?
推荐答案
您尝试做的事情称为展平列表.根据 Python 之禅,您正在尝试做正确的事情.引用自
What you are trying to do is called flattening the list. And according to the Zen of Python, you are trying to do the right thing. Quoting from that
扁平优于嵌套.
所以你可以像这样使用列表理解
So you can use list comprehension like this
newlist = [item for items in newlist for item in items]
或者你可以像这样使用 itertools
中的 chain
from itertools import chain
newlist = list(chain(*newlist))
或者你可以使用chain.from_iterable
,这里不需要解包列表
from itertools import chain
newlist = list(chain.from_iterable(newlist))
使用sum
函数
newlist = sum(newlist, [])
使用reduce
函数
newlist = reduce(lambda x,y: x+y, newlist)
使用 operator.add
.这将比带有 lambda
版本的 reduce
更快.
Using operator.add
. This will be faster than the reduce
with lambda
version.
import operator
newlist = reduce(operator.add, newlist)
为了完整起见,包括在 也可以从 Python 中的列表列表中制作一个平面列表.
For the sake of completeness, included the answers found in Making a flat list out of list of lists in Python as well.
我尝试在 Python 2.7 中为它们计时,就像这样
I tried to time all of them in Python 2.7, like this
from timeit import timeit
print(timeit("[item for items in newlist for item in items]", "from __main__ import newlist"))
print(timeit("sum(newlist, [])", "from __main__ import newlist"))
print(timeit("reduce(lambda x,y: x+y, newlist)", "from __main__ import newlist"))
print(timeit("reduce(add, newlist)", "from __main__ import newlist; from operator import add"))
print(timeit("list(chain(*newlist))", "from __main__ import newlist; from itertools import chain"))
print(timeit("list(chain.from_iterable(newlist))", "from __main__ import newlist; from itertools import chain"))
在我的机器上输出
2.26074504852
2.45047688484
3.50180387497
2.56596302986
1.78825688362
1.61612296104
因此,最有效的方法是在 Python 2.7 中使用 list(chain.from_iterable(newlist))
.在 Python 3.3
So, the most efficient way to do this is to use list(chain.from_iterable(newlist))
, in Python 2.7. Ran the same test in Python 3.3
from timeit import timeit
print(timeit("[item for items in newlist for item in items]", "from __main__ import newlist"))
print(timeit("sum(newlist, [])", "from __main__ import newlist"))
print(timeit("reduce(lambda x,y: x+y, newlist)", "from __main__ import newlist; from functools import reduce"))
print(timeit("reduce(add, newlist)", "from __main__ import newlist; from operator import add; from functools import reduce"))
print(timeit("list(chain(*newlist))", "from __main__ import newlist; from itertools import chain"))
print(timeit("list(chain.from_iterable(newlist))", "from __main__ import newlist; from itertools import chain"))
在我的机器上输出
2.26074504852
2.45047688484
3.50180387497
2.56596302986
1.78825688362
1.61612296104
因此,无论是 Python 2.7 还是 3.3,使用 list(chain.from_iterable(newlist))
来展平嵌套列表.
So, be it Python 2.7 or 3.3, use list(chain.from_iterable(newlist))
to flatten the nested lists.
这篇关于如何展平列表/嵌套列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!