>>> from numpy import *
>>> a = arange(12)**2
>>> a
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121])
>>> i=array([1,1,3,8,5])
>>> a11385=a[i]
>>> a11385
array([ 1, 1, 9, 64, 25])
>>> j = array( [ [ 3, 4], [ 9, 7 ] ] )
>>> j
array([[3, 4],
[9, 7]])
>>> a
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121])
>>> a[j]
array([[ 9, 16],
[81, 49]])
>>>
NumPy比普通Python序列提供更多的索引功能。除了索引整数和切片,正如我们之前看到的,数组可以被整数数组和布尔数组索引。
>>> a = arange(12)**2 # the first 12 square numbers
>>> i = array( [ 1,1,3,8,5 ] ) # an array of indices
>>> a[i] # the elements of a at the positions i
array([ 1, 1, 9, 64, 25])
>>>
>>> j = array( [ [ 3, 4], [ 9, 7 ] ] ) # a bidimensional array of indices
>>> a[j] # the same shape as j
array([[ 9, 16],
[81, 49]])