2

I have several NumPy arrays which I wanted to remove the column in each arrays. The index of the columsn is same for all arrays.

I wrote this code and it didn't work.

list= [a1, a2, a3]

for arrry in list:
        arrry  = np.delete(arrry, [0, 1, 2], axis=1)

2 Answers 2

1

You need to remember that Python is call-by-name. When you do

arrry = np.delete(arrry, [0, 1, 2], axis=1)

you're assigning the name arrry to the new array with the missing column, but are not changing the list. try:

list= [a1, a2, a3]

for idx, arrry in enumerate(list):
        list[idx] = np.delete(arrry, [0, 1, 2], axis=1)
Sign up to request clarification or add additional context in comments.

2 Comments

I realized that, when I call "a1" again, it not showing the modified array. Instead, list[0] will show. Is there any way to simply call a1?
Not really, because np.delete returns a new array, hance list[0] no longer points to the same array as a1. I don't know what your complete code is supposed to do, but chances are you should probably use the list from the start, rather than using a1, a2, a3 separately.
1

you can do a list comprehension,

lst = [np.arange(100,112).reshape(2,6),np.arange(12).reshape(2,6)]
>>>lst
[array([[100, 101, 102, 103, 104, 105],
       [106, 107, 108, 109, 110, 111]]), 
 array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
]

lst = [np.delete(x, [0, 1, 2], axis=1)for x in lst]
>>>lst
[array([[103, 104, 105],
        [109, 110, 111]]), 
 array([[ 3,  4,  5],
        [ 9, 10, 11]])
]

Comments

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.