Python 如何展平列表/嵌套列表列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20112776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:35:16  来源:igfitidea点击:

How do I flatten a list of lists/nested lists?

pythonpython-2.7

提问by user2879220

I have python code like this:

我有这样的python代码:

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]]

I want the newlistlike:

我想要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 ?

有什么办法可以解决吗?

回答by tmj

temp = []
for small_list in newlist:
    temp += small_list
newlist = temp

This should do it.

这应该这样做。

回答by thefourtheye

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

您正在尝试做的称为展平列表。根据Python,您正在尝试做正确的事情。引用那个

Flat is better than nested.

扁平比嵌套好。

  1. So you can use list comprehension like this

    newlist = [item for items in newlist for item in items]
    
  2. Or you can use chainfrom itertoolslike this

    from itertools import chain
    newlist = list(chain(*newlist))
    
  3. Or you can use chain.from_iterable, where unpacking of the list is not necessary

    from itertools import chain
    newlist = list(chain.from_iterable(newlist))
    
  4. Using sumfunction

    newlist = sum(newlist, [])
    
  5. Using reducefunction

    newlist = reduce(lambda x,y: x+y, newlist)
    
  6. Using operator.add. This will be faster than the reducewith lambdaversion.

    import operator
    newlist = reduce(operator.add, newlist)
    
  1. 所以你可以像这样使用列表理解

    newlist = [item for items in newlist for item in items]
    
  2. 或者您可以使用chainitertools这样的

    from itertools import chain
    newlist = list(chain(*newlist))
    
  3. 或者您可以使用chain.from_iterable,在不需要解包列表的情况下

    from itertools import chain
    newlist = list(chain.from_iterable(newlist))
    
  4. 使用sum功能

    newlist = sum(newlist, [])
    
  5. 使用reduce功能

    newlist = reduce(lambda x,y: x+y, newlist)
    
  6. 使用operator.add. 这将比reducewithlambda版本更快。

    import operator
    newlist = reduce(operator.add, newlist)
    

Edit:For the sake of completeness, included the answers found in Making a flat list out of list of lists in Pythonas well.

编辑:为了完整起见,还包括在从 Python 中的列表列表制作平面列表中找到的答案。

I tried to time all of them in Python 2.7, like this

我试图在Python 2.7 中为所有这些计时,就像这样

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"))

Output on my machine

在我的机器上输出

2.26074504852
2.45047688484
3.50180387497
2.56596302986
1.78825688362
1.61612296104

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

因此,最有效的方法是list(chain.from_iterable(newlist))在 Python 2.7 中使用, 。在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"))

Output on my machine

在我的机器上输出

2.26074504852
2.45047688484
3.50180387497
2.56596302986
1.78825688362
1.61612296104

So, be it Python 2.7 or 3.3, use list(chain.from_iterable(newlist))to flatten the nested lists.

因此,无论是 Python 2.7 还是 3.3,都可以list(chain.from_iterable(newlist))用来展平嵌套列表。

回答by Christian

Try:

尝试:

newlist = [j for i in newlist for j in i]

回答by lennon310

Just the easiest one:

最简单的一个:

newlist = sum(newlist, [])
print newlist