0

I can use an array or a list to index into numpy.array, e.g.:

a = np.array([1, 2, 3, 4])
print(a[[1, 3]])

will produce

[2 4]

Is there an equivalent construct to index into a standard Python list or array?

Just to be more specific: indexing has to be with an array and indexing pattern is random, i.e. not possible with slicing. Here is a better example:

a = np.array([1, 2, 3, 4])
i = [3, 0, 1]
print(a[i])
.....
[4 1 2]
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72

1 Answers1

1
from operator import itemgetter 

print(itemgetter(1,3)(a))

and to turn it into a list:

print(list(itemgetter(1,3)(a)))