# #索引与切片

Indexing routines

## #单个元素索引

``````>>> x = np.arange(10)
>>> x[2]
2
>>> x[-2]
8``````

``````>>> x.shape = (2,5) # now x is 2-dimensional
>>> x[1,3]
8
>>> x[1,-1]
9``````

``````>>> x[0]
array([0, 1, 2, 3, 4])``````

``````>>> x[0][2]
2``````

## #其他索引选项

``````>>> x = np.arange(10)
>>> x[2:5]
array([2, 3, 4])
>>> x[:-7]
array([0, 1, 2])
>>> x[1:7:2]
array([1, 3, 5])
>>> y = np.arange(35).reshape(5,7)
>>> y[1:5:2,::3]
array([[ 7, 10, 13],
[21, 24, 27]])``````

## #索引数组

Numpy数组可以被其他数组（或任何其他可转换为数组的类似序列的对象，例如除了元组之外的列表）索引；有关为什么会出现这种情况，请参阅本文档的末尾。索引数组的使用范围从简单，直接的情况到复杂的难以理解的情况。对于索引数组的所有情况，返回的是原始数据的副本，而不是片段获取的视图。

``````>>> x = np.arange(10,1,-1)
>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])``````

``````>>> x[np.array([3,3,-3,8])]
array([7, 7, 4, 2])``````

``````>>> x[np.array([3, 3, 20, 8])]
<type 'exceptions.IndexError'>: index 20 out of bounds 0<=index<9``````

``````>>> x[np.array([[1,1],[2,3]])]
array([[9, 9],
[8, 7]])``````

## #索引多维数组

``````>>> y[np.array([0,2,4]), np.array([0,1,2])]
array([ 0, 15, 30])``````

``````>>> y[np.array([0,2,4]), np.array([0,1])]
<type 'exceptions.ValueError'>: shape mismatch: objects cannot be``````

broadcast to a single shape

``````>>> y[np.array([0,2,4]), 1]
array([ 1, 15, 29])``````

``````>>> y[np.array([0,2,4])]
array([[ 0,  1,  2,  3,  4,  5,  6],
[14, 15, 16, 17, 18, 19, 20],
[28, 29, 30, 31, 32, 33, 34]])``````

## #布尔值或掩码索引数组

``````>>> b = y>20
>>> y[b]
array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34])``````

``````>>> b[:,5] # use a 1-D boolean whose first dim agrees with the first dim of y
array([False, False, False,  True,  True], dtype=bool)
>>> y[b[:,5]]
array([[21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 32, 33, 34]])``````

``````>>> x = np.arange(30).reshape(2,3,5)
>>> x
array([[[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
>>> b = np.array([[True, True, False], [False, True, True]])
>>> x[b]
array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]])``````

## #组合索引和切片

``````>>> y[np.array([0,2,4]),1:3]
array([[ 1,  2],
[15, 16],
[29, 30]])``````

``````>>> y[b[:,5],1:3]
array([[22, 23],
[29, 30]])``````

## #结构化索引工具

``````>>> y.shape
(5, 7)
>>> y[:,np.newaxis,:].shape
(5, 1, 7)``````

``````>>> x = np.arange(5)
>>> x[:,np.newaxis] + x[np.newaxis,:]
array([[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])``````

``````>>> z = np.arange(81).reshape(3,3,3,3)
>>> z[1,...,2]
array([[29, 32, 35],
[38, 41, 44],
[47, 50, 53]])``````

``````>>> z[1,:,:,2]
array([[29, 32, 35],
[38, 41, 44],
[47, 50, 53]])``````

## #给被索引的数组赋值

``````>>> x = np.arange(10)
>>> x[2:7] = 1``````

``>>> x[2:7] = np.arange(5)``

``````>>> x[1] = 1.2
>>> x[1]
1
>>> x[1] = 1.2j
<type 'exceptions.TypeError'>: can't convert complex to long; use
long(abs(z))``````

``````>>> x = np.arange(0, 50, 10)
>>> x
array([ 0, 10, 20, 30, 40])
>>> x[np.array([1, 1, 3, 1])] += 1
>>> x
array([ 0, 11, 20, 31, 40])``````

## #处理程序中可变数量的索引

``````>>> indices = (1,1,1,1)
>>> z[indices]
40``````

``````>>> indices = (1,1,1,slice(0,2)) # same as [1,1,1,0:2]
>>> z[indices]
array([39, 40])``````

``````>>> indices = (1, Ellipsis, 1) # same as [1,...,1]
>>> z[indices]
array([[28, 31, 34],
[37, 40, 43],
[46, 49, 52]])``````

``````>>> z[[1,1,1,1]] # produces a large array
array([[[[27, 28, 29],
[30, 31, 32], ...
>>> z[(1,1,1,1)] # returns a single value
40``````