Wellington Júnior - 2 months ago 5x

Python Question

So, I was trying to understand the

`numpy.delete`

`>>>import numpy as np`

>>>a = np.arange(10)

>>> a

array([0, 1, 2, 3, 4, 6, 7, 9])

>>> a[5]

5

>>> a=np.delete(a,[a[5]])

>>> a

array([0, 1, 2, 3, 4, 6, 7, 8, 9]) #so far so good

>>> a[6]

7

>>> a=np.delete(a,[a[6]])

>>> a

array([0, 1, 2, 3, 4, 6, 7, 9])

So... When I put

`a=np.delete(a,[a[6]])`

`7`

`8`

`a[7]`

`a[6]`

I also noticed that when I try to remove the

`a[0]`

Answer

The second argument should be the **index** of the element you want to delete, not the element itself.

```
a=np.delete(a,6)
```

In the first case, it only worked because a[5] happened to equal 5, so the index and the value were the same.

When you have:

```
a=np.delete(a,[a[6]])
```

You are deleting the 7th element since a[6] = 7 there.

Source (Stackoverflow)

Comments