Convert multi-dimensional list to a 1D list in Python

前端 未结 7 700
萌比男神i
萌比男神i 2020-12-05 00:14

A multidimensional list like l=[[1,2],[3,4]] could be converted to a 1D one by doing sum(l,[]). Can anybody please explain how that happens?

<
7条回答
  •  盖世英雄少女心
    2020-12-05 00:39

    It looks to me more like you're looking for a final answer of:

    [3, 7]
    

    For that you're best off with a list comprehension

    >>> l=[[1,2],[3,4]]
    >>> [x+y for x,y in l]
    [3, 7]
    

提交回复
热议问题