Home > Enterprise >  How do assign a specific part of a Numpy array?
How do assign a specific part of a Numpy array?

Time:11-15

This is what I want to do:

a = [[1, 2],[3,4]]
b = np.zeros(shape = (2,2))
b[:, 1:] = a[:,1:]

But I get this error message:

TypeError: list indices must be integers or slices, not tuple

CodePudding user response:

As @MattDMo has mentioned, you are trying to use numpy slicing on a 2D list. You can fix this by simply converting the list to a numpy array

a = np.array([[1, 2],[3,4]])
b = np.zeros(shape = (2,2))
b[:, 1:] = a[:,1:]
  • Related