Reputation: 102
import numpy as np
m = np.arange(10).tolist()
n = [2, 6, 4]
I want to delete the 2nd, 6th, and 4th elements in list m.
del m[n]
Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers or slices, not list
I tried this:
ns = np.sort(n)
for i in np.arange(len(ns)):
m.pop(ns[i] - i)
but it pop out the deleted elements Is there any elegant method to do this job?
Upvotes: 0
Views: 46
Reputation: 81
For this simple case, you can use m = np.delete(m, n)
.
Here is a link to the doc : https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html
Upvotes: 1