Numpy delete method doing strange thing

Asked

Viewed 57 times

1

i was testing the delete method of the array class of the numpy library and a strange thing happened: I had the element of Indice 0 deleted from the array, but the element of Indice 1 is deleted. I don’t understand what this delete is doing

>>> import numpy as np
>>> lista = np.array([1,2,3,4,5,6])
>>> lista
array([1, 2, 3, 4, 5, 6])
>>> lista[0]
1
>>> lista = np.delete(lista,lista[0])
>>> lista
array([1, 3, 4, 5, 6])

1 answer

1


The delete numpy library removes a sub-array based on the index and not the value of the element to remove.

In your example what you want to do is:

lista = np.delete(lista,0) #indice 0 a ser removido
print(lista) #[2 3 4 5 6]

You can even pass a list of indices to be removed by doing:

lista = np.delete(lista,[0,1,3]) #indice 0, 1 e 3 a serem removidos
print(lista) #[3 5 6]

See both examples in Ideone

Documentation for np.delete

Browser other questions tagged

You are not signed in. Login or sign up in order to post.