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

时间:2013-11-21 04:52:35

标签: python python-2.7

我有这样的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]]

我希望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]

有什么方法可以解决吗?

4 个答案:

答案 0 :(得分:36)

您要做的事情称为展平列表。根据{{​​3}},你正在努力做正确的事。引用那个

  

Flat比嵌套好。

  1. 所以你可以像这样使用列表理解

    newlist = [item for items in newlist for item in items]
    
  2. 或者您可以使用chain中的itertools

    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。这比reducelambda更快。

    import operator
    newlist = reduce(operator.add, newlist)
    
  7. 修改:为了完整起见,还包括Zen of Python中的答案。

    我尝试在 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"))
    

    我机器上的输出

    2.26074504852
    2.45047688484
    3.50180387497
    2.56596302986
    1.78825688362
    1.61612296104
    

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

答案 1 :(得分:3)

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

这应该这样做。

答案 2 :(得分:2)

尝试:

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

答案 3 :(得分:2)

最简单的一个:

newlist = sum(newlist, [])
print newlist