0

I want to do the following using numpy:

  • create an an array of arrays using numpy where each row contains only one element such as

[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]]

xx = np.array([np.array([0])] * 10)

  • append an element to a specific row such as

[[0],[0,5],[0],[0],[0],[0],[0],[0],[0],[0]]

xx[1] = np.append(xx[1],5)

  • retrieve an element from a specific row such as

print(x[1,1])

this means that I need a two dimensional array with different row size and the elements are appended dynamically

3
  • That is not the kind of thing NumPy is designed for. Consider using a list of lists. Commented Oct 13, 2018 at 19:55
  • Thanks. So I'll get back to using normal lists Commented Oct 13, 2018 at 20:14
  • Possible duplicate of List of lists changes reflected across sublists unexpectedly Commented Oct 13, 2018 at 20:32

1 Answer 1

1

If using a lists inside a list, you can create it like this

l = [[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]]

And if you want to append something, just use

l[1].append(4)

You will get:

[[0],[0,4],[0],[0],[0],[0],[0],[0],[0],[0]]

And if you want to access the new element:

l[1][1]

Which will return:

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

2 Comments

Thanks. But when I use l = [[0]]*10 instead of l = [[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]] the second line (l[1].append(4)) adds the value 4 to all generating [[0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4]]

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.