How can I convert a 3D list into a 2D list in python?

后端 未结 3 712
北海茫月
北海茫月 2021-01-21 23:58

This is the code that I have:

[[[3], [4]], [[5], [6]], [[7], [8]]]

How can I change it into:

[[3], [4], [5], [6], [7], [8]]


        
相关标签:
3条回答
  • 2021-01-22 00:17

    try this:

    import numpy as np
    a = np.array([[[3],[4]],[[5],[6]],[[7],[8]]])
    b = a.reshape(6,1)
    
    0 讨论(0)
  • 2021-01-22 00:18

    A uncompressed or long way:

    l3d = [[[3], [4]], [[5], [6]], [[7], [8]]]
    l2d = []
    for e1 in l3d:
       for e2 in e1:
          l2d.append(e2)
    
    0 讨论(0)
  • 2021-01-22 00:33

    You want to flatten a single level of the input list, try this solution using a list comprehension:

    lst = [[[3], [4]], [[5], [6]], [[7], [8]]]
    [e for sl in lst for e in sl]
    => [[3], [4], [5], [6], [7], [8]]
    
    0 讨论(0)
提交回复
热议问题