For performance, use
a[b[0], b[1]]
a[b[:,0], b[:,1]] # when b is a 2d array
unless d = tuple(b.T)
is readily available (a[d]
is the fastest)
time bench shows a[b[:,0], b[:,1]]
is significantly faster than a[tuple(c)]
both when b is 1d and 2d (omitted), where c == b.T
import numpy as np
import time
a = np.array([[1, 2], [3, 4]])s
b = np.array([1,1])
c = b.T # a[b[:,0], b[:,1]] == a[tuple(c)] when b is 2d
assert tuple(c) == tuple(b)
d = tuple(b)
t0 = time.time()
for _ in range(10000000):
a[tuple(c)]
t1 = time.time()
print(t1 - t0) # 7.781806468963623
t0 = time.time()
for _ in range(10000000):
a[b[0], b[1]]
t1 = time.time()
print(t1 - t0) # 2.8317997455596924
t0 = time.time()
for _ in range(10000000):
a[*b]
t1 = time.time()
print(t1 - t0) # 7.80025315284729
t0 = time.time()
for _ in range(10000000):
a[d]
t1 = time.time()
print(t1 - t0) # 1.3347711563110352