Converting a 3D List to a 3D NumPy array

前端 未结 5 1039
北海茫月
北海茫月 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:52

    The answers by @SonderingNarcissit and @MadPhysicist are already quite nice.

    Here is a quick way of adding a number to each element in your list and keeping the structure. You can replace the function return_number by anything you like, if you want to not only add a number but do something else with it:

    def return_number(my_number):
        return my_number + 4    
    
    def add_number(my_list):
    
        if isinstance(my_list, (int, float)):
            return return_number(my_list)
        else:
            return [add_number(xi) for xi in my_list]
    
    A = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0], [0], [0]]]
    

    Then

    print(add_number(A))
    

    gives you the desired output:

    [[[4, 4, 4], [4, 4, 4], [4, 4, 4]], [[4], [4], [4]]]
    

    So what it does is that it look recursively through your list of lists and everytime it finds a number it adds the value 4; this should work for arbitrarily deep nested lists. That currently only works for numbers and lists; if you also have e.g. also dictionaries in your lists then you would have to add another if-clause.

提交回复
热议问题