Initialise numpy array of unknown length

前端 未结 5 1149
无人及你
无人及你 2020-12-02 08:38

I want to be able to \'build\' a numpy array on the fly, I do not know the size of this array in advance.

For example I want to do something like this:



        
5条回答
  •  悲&欢浪女
    2020-12-02 09:15

    Build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to array, for a total of O(n).

        a = []
        for x in y:
            a.append(x)
        a = np.array(a)
    

提交回复
热议问题