Reputation: 111
I stuck with a simple question in NumPy. I have an array of zero values. Once I generate a new value I would like to add it one by one.
arr=array([0,0,0])
# something like this
l=[1,5,10]
for x in l:
arr.append(x) # from python logic
so I would like to add one by one x into array, so I would get: 1st iteration arr=([1,0,0])
; 2d iteration arr=([1,5,0])
; 3rd arr=([1,5,10])
;
Basically I need to substitute zeros with new values one by one in NumPy (I am learning NumPy!!!!!!). I checked many of NumPy options like np.append (it adds to existing values new values), but can't find the right.
thank you
Upvotes: 1
Views: 857
Reputation: 404
There are a few things to pick up with numpy:
you can generate the array full of zeros with
>>> np.zeros(3)
array([ 0., 0., 0.])
You can get/set array elements with indexing as with list
s etc:
arr[2] = 7
for i, val in enumerate([1, 5, 10]):
arr[i] = val
Or, if you want to fill with array with something like a list
, you can directly use:
>>> np.array([1, 5, 10])
array([ 1, 5, 10])
Also, numpy's signature for appending stuff to an array is a bit different:
arr = np.append(arr, 7)
Having said that, you should just consider diving into Numpy's own userguide.
Upvotes: 1