How to add a new row to an empty numpy array

前端 未结 6 583
旧时难觅i
旧时难觅i 2020-11-28 01:13

Using standard Python arrays, I can do the following:

arr = []
arr.append([1,2,3])
arr.append([4,5,6])
# arr is now [[1,2,3],[4,5,6]]

Howev

6条回答
  •  失恋的感觉
    2020-11-28 01:43

    In case of adding new rows for array in loop, Assign the array directly for firsttime in loop instead of initialising an empty array.

    for i in range(0,len(0,100)):
        SOMECALCULATEDARRAY = .......
        if(i==0):
            finalArrayCollection = SOMECALCULATEDARRAY
        else:
            finalArrayCollection = np.vstack(finalArrayCollection,SOMECALCULATEDARRAY)
    

    This is mainly useful when the shape of the array is unknown

提交回复
热议问题