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

user3631926
  • 162
  • 1
  • 12

1 Answers1

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
DanDeg
  • 316
  • 1
  • 2
  • 7
  • 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]] – user3631926 Oct 13 '18 at 20:13
  • @user3631926: https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – user2357112 Oct 13 '18 at 20:15