How to build a numpy array row by row in a for loop?

前端 未结 2 1135
难免孤独
难免孤独 2021-01-19 10:45

This is basically what I am trying to do:

array = np.array()       #initialize the array. This is where the error code described below is thrown

for i in xr         


        
2条回答
  •  清酒与你
    2021-01-19 11:22

    Use a list of 1D numpy arrays, or a list of lists, and then convert it to a numpy 2D array (or use more nesting and get more dimensions if you need to).

    import numpy as np
    
    a = []
    for i in range(5):
        a.append(np.array([1,2,3])) # or a.append([1,2,3])
    a = np.asarray(a) # a list of 1D arrays (or lists) becomes a 2D array
    
    print(a.shape)
    print(a)
    

提交回复
热议问题