索引可以以列表的形式给出。假如说selections
长度与choices
:
b = numpy.array(choices)
result = b[range(len(selections)), selections]
将给出由选择中的索引指定的选择中的值。看看它的实际效果:
numpy.random.seed(1)
b = numpy.random.randint(0,100,(5,10))
>>> array([[37, 12, 72, 9, 75, 5, 79, 64, 16, 1],
[76, 71, 6, 25, 50, 20, 18, 84, 11, 28],
[29, 14, 50, 68, 87, 87, 94, 96, 86, 13],
[ 9, 7, 63, 61, 22, 57, 1, 0, 60, 81],
[ 8, 88, 13, 47, 72, 30, 71, 3, 70, 21]])
selections = numpy.random.randint(0,10,5)
>>> array([1, 9, 3, 4, 8])
result = b[range(len(selections)),selections]
>>>> array([12, 28, 68, 22, 70])