Converting a 3D List to a 3D NumPy array

前端 未结 5 1038
北海茫月
北海茫月 2021-01-18 16:19

Currently, I have a 3D Python list in jagged array format.
A = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0], [0], [0]]]

Is there any way I could convert

5条回答
  •  后悔当初
    2021-01-18 16:46

    It is unfortunate that the input structure is a jagged list. If one could adjust the method used to generate the list by assigning no data values, then there is so much more one can do. I made this comment in the initial post, but I will demonstrate how the design of the originals could be altered to facilitate obtaining more data while enabling the return of a list.

    I have done this as a function so I could comment the inputs and outputs for further reference.

    def num_46():
        """(num_46)... Masked array from ill-formed list
        :  http://stackoverflow.com/questions/40289943/
        :  converting-a-3d-list-to-a-3d-numpy-array
        :  A =[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], 
        :      [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0], [0], [0]]]
        """
        frmt = """
        :Input list...
        {}\n
        :Masked array data
        {}\n
        :A sample calculations:
        :  a.count(axis=0) ... a.count(axis=1) ... a.count(axis=2)
        {}\n
        {}\n
        {}\n
        : and finally:  a * 2
        {}\n
        :Return it to a list...
        {}
        """
        a_list = [[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
                  [[9, 10, 11], [12, 13, 14], [15, 16, 17]],
                  [[18, -1, -1], [21, -1, -1], [24, -1, -1]]]
        mask_val = -1
        a = np.ma.masked_equal(a_list, mask_val)
        a.set_fill_value(mask_val)
        final = a.tolist(mask_val)
        args = [a_list, a,
                a.count(axis=0), a.count(axis=1), a.count(axis=2),
                a*2, final]
        print(dedent(frmt).format(*args))
        return a_list, a, final
    
    
    #----------------------
    if __name__ == "__main__":
        """Main section...   """
        A, a, c = num_46()
    

    Some results that show that the use of masked arrays may be preferable to jagged/malformed list structure.

    :Input list...
    [[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
     [[9, 10, 11], [12, 13, 14], [15, 16, 17]],
     [[18, -1, -1], [21, -1, -1], [24, -1, -1]]]
    
    :Masked array data
    [[[0 1 2]
      [3 4 5]
      [6 7 8]]
    
     [[9 10 11]
      [12 13 14]
      [15 16 17]]
    
     [[18 - -]
      [21 - -]
      [24 - -]]]
    
    :A sample calculations:
    :  a.count(axis=0) ... a.count(axis=1) ... a.count(axis=2)
    [[3 2 2]
     [3 2 2]
     [3 2 2]]
    
    [[3 3 3]
     [3 3 3]
     [3 0 0]]
    
    [[3 3 3]
     [3 3 3]
     [1 1 1]]
    
    : and finally:  a * 2
    [[[0 2 4]
      [6 8 10]
      [12 14 16]]
    
     [[18 20 22]
      [24 26 28]
      [30 32 34]]
    
     [[36 - -]
      [42 - -]
      [48 - -]]]
    
    :Return it to a list...
    [[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, -1, -1], [21, -1, -1], [24, -1, -1]]]
    

    Hope this helps someone.

提交回复
热议问题