1

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

1 Answer 1

1

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 lists 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.