Efficient way for appending numpy array

前端 未结 3 1984
谎友^
谎友^ 2021-01-05 21:57

I will keep it simple.I have a loop that appends new row to a numpy array...what is the efficient way to do this.

n=np.zeros([1,2])
for x in [[2,3],[4,5],[7,         


        
3条回答
  •  醉酒成梦
    2021-01-05 22:25

    You might want to try:

    import numpy as np
    
    n = np.reshape([], (0, 2))
    for x in [[2,3],[4,5],[7,6]]:
          n = np.append(n, [x], axis=0)
    

    Instead of np.append you can also use n = np.vstack([n,x]). I also agree with @Bi Rico that I also would use a list, if n does not need to accessed within the loop.

提交回复
热议问题