Home > OS >  numpy array not changing value on assignment
numpy array not changing value on assignment

Time:05-27

I have the following python program.

import numpy as np
x = np.array([[1,2],[3,5],[4,6]])
some_list = [1,2]
x[some_list][:,1]=100

The numpy array "x" still remains unchanged after the above execution.
The above code is a small working example of what I want to do!
So, I have a list (of indices) and a numpy array.
I want to change a column (for example column with index 1 in the above program) in all rows indexed by the list.
What is the smallest and the easiest to understand piece of code doing this?
The above code doesn't work as expected, why so?
Thanks in advance for answering!

CodePudding user response:

You need to combine the different pairs of []:

x[some_list, 1] = 100

Output:

>>> x
array([[  1,   2],
       [  3, 100],
       [  4, 100]])
  • Related