Merge multiple 2d lists considering axis in order

前端 未结 3 1777
予麋鹿
予麋鹿 2021-01-13 11:50

My purpose is to combine multiple 2d list in order such as:

a = [[1,2],[3,1]]
b= [[3,6],[2,9]]
c = [[5,1],[8,10]]
Expected: [[1,2,3,6,5,1],[3,1,2,9,8,10]]
         


        
3条回答
  •  不知归路
    2021-01-13 12:31

    You can use zip and a list comprehension:

    >>> a = [[1,2],[3,1]]
    >>> b = [[3,6],[2,9]]
    >>> c = [[5,1],[8,10]]
    >>> [x+y+z for x,y,z in zip(a, b, c)]
    [[1, 2, 3, 6, 5, 1], [3, 1, 2, 9, 8, 10]]
    >>>
    

提交回复
热议问题