user4159592
user4159592

Reputation:

Append Numpy Array post Creation

After creating a numpy array, I'm looking to append post creation:

numpy_array = np.zeros(2,3)

numpy_array[0][1].append(4,5)

Where the output for numpy_array[0][1] would be [0,4,5]

What is the best way to do this?

Upvotes: 1

Views: 51

Answers (2)

Shapi
Shapi

Reputation: 5593

If you create a numpyp.zeros(2,3) you will get a TypeError (Posting bellow with the code the correct way to use it).

You can copy to numpy array of [0. 0. 0.] the [4, 5] list using:

np.zeros([2,3]) #note the arg is a list type 
numpy_array[0][1:3] = [4,5]

This [1:3] get the positions interval.

Upvotes: 0

Shahriar
Shahriar

Reputation: 13806

You can create 2d array of any type like this:

Matrix = [[0 for x in range(5)] for x in range(5)] 

For your purpose:

>>> Matrix = [[ [0] for x in range(3)] for x in range(2)] 
>>> Matrix[0][1]+=[4,5]
>>> Matrix
[[[0], [0, 4, 5], [0]], [[0], [0], [0]]]

Upvotes: 1

Related Questions