How to extend an array in-place in Numpy?

后端 未结 4 984
南旧
南旧 2020-11-27 18:29

Currently, I have some code like this

import numpy as np
ret = np.array([])
for i in range(100000):
  tmp =  get_input(i)
  ret = np.append(ret, np.zeros(len         


        
4条回答
  •  时光取名叫无心
    2020-11-27 18:42

    The usual way to handle this is something like this:

    import numpy as np
    ret = []
    for i in range(100000):
      tmp =  get_input(i)
      ret.append(np.zeros(len(tmp)))
      ret.append(np.zeros(fixed_length))
    ret = np.concatenate(ret)
    

    For reasons that other answers have gotten into, it is in general impossible to extend an array without copying the data.

提交回复
热议问题