0

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?

0

1 Answer 1

1

You need to combine the different pairs of []:

x[some_list, 1] = 100

Output:

>>> x
array([[  1,   2],
       [  3, 100],
       [  4, 100]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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