1

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?

6
  • 'numpy.int32' object has no attribute 'append' Commented Oct 28, 2015 at 18:37
  • Is there any way to create a set of arrays you can later append additional values to with numpy? Commented Oct 28, 2015 at 18:37
  • Do you need integer array? Commented Oct 28, 2015 at 18:39
  • I could use integer or string but prefer integer Commented Oct 28, 2015 at 18:40
  • @AndreL, Did you check that? Commented Oct 28, 2015 at 18:53

2 Answers 2

1

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]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this answer uses Python lists, not numpy arrays. And for this task that probably a good thing.
0

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.

Comments

Your Answer

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