# NumPy 陣列索引

``````>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> a[0]
1
>>> a[2:3]
array([3])
>>> a[3:]
array([4, 5])
>>> a[:3]
array([1, 2, 3])
>>> a[:]
array([1, 2, 3, 4, 5])
>>>
``````

``````>>> b = a[3:]
>>> b[0] = 10
>>> a
array([ 1,  2,  3, 10,  5])
>>>
``````

``````>>> c = [1, 2, 3]
>>> c = [1, 2, 3, 4, 5]
>>> d = c[3:]
>>> d[0] = 10
>>> c
[1, 2, 3, 4, 5]
>>>
``````

``````>>> e = np.arange(9).reshape((3, 3))
>>> e1 = e[1][1:3]
>>> e2 = e[1,1:3]
>>> e1
array([4, 5])
>>> e2
array([4, 5])
>>>
``````

``````>>> e3 = e[0:2][1]
>>> e4 = e[0:2,1]
>>> e3
array([3, 4, 5])
>>> e4
array([1, 4])
>>>
``````

`e3` 應該沒問題，`e[0:2]` 取得一個二維陣列，再取它的索引 1，`e4` 呢？`e[0:2,1]` 的寫法，是指 axis 0 索引範圍 0 到 2（如下圖藍色）與 axis 1 索引 1 到 2 交叉的部份（如下圖綠色），看看下圖，就會知道為何結果會是 `[1, 4]`

``````a = [1, 2, 3, 4, 5]
b = []
for i in range(len(a)):
if i in [0, 1, 4]:
b.append(a[i])
``````

NumPy 的 `[]` 可以指定索引陣列，例如：

``````>>> a = np.arange(1, 6)
>>> a[[0, 1, 4]]
array([1, 2, 5])
>>>
``````

``````>>> a = np.arange(25).reshape((5, 5))
>>> a[[0, 1, 4], [0, 3, 4]]
array([ 0,  8, 24])
>>>
``````

``````>>> a = np.arange(25).reshape((5, 5))
>>> b = a[[0, 1, 4], [0, 3, 4]]
>>> a
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]])
>>> b
array([ 0,  8, 24])
>>> b[1] = 10
>>> b
array([ 0, 10, 24])
>>> a
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]])
>>>
``````

``````[[ 0  3  4]
[ 5  8  9]
[20 23 24]]
``````

``````>>> a = np.arange(25).reshape((5, 5))
>>> a[np.ix_([0, 1, 4], [0, 3, 4])]
array([[ 0,  3,  4],
[ 5,  8,  9],
[20, 23, 24]])
>>>
``````

``````>>> a = np.arange(1, 6)
>>> a[[True, True, False, False, True]]
array([1, 2, 5])
>>>
``````

``````>>> a = np.arange(25).reshape((5, 5))
>>> a[(
...     [True, True, False, False, True],
...     [True, False, True, False, True]
... )]
array([ 0,  7, 24])
>>>
``````

``````[
[ 0,  2, 4],
[ 5, 7, 9],
[20, 22, 24]
]
``````

``````>>> a[np.ix_(
...     [True, True, False, False, True],
...     [True, False, True, False, True]
... )]
array([[ 0,  2,  4],
[ 5,  7,  9],
[20, 22, 24]])
>>>
``````

``````>>> a = np.array([1, 2, 3, 4, 5])
>>> a[[1, 2, 3]] = 10
>>> a
array([ 1, 10, 10, 10,  5])
>>> a[[1, 2, 3]] = [100, 200, 300]
>>> a
array([  1, 100, 200, 300,   5])
>>>
``````

Python 3 以後，有個 `...` 語法可以使用，代表 `Ellipsis` 物件：

``````>>> ...
Ellipsis
>>> Ellipsis
Ellipsis
>>>
``````

`...` 代表省略之意，在 NumPy 的實作中，可以使用 `Ellipsis` 物件來進行切片，代表略過某些維度，例如，若有個陣列代表圖片像素：

``````>>> img = np.array([
...     [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
...     [[124, 255, 23], [245, 222, 255], [255, 232, 255]],
...     [[135, 255, 23], [245, 123, 255], [255, 23, 35]]
... ])
>>> img.shape
(3, 3, 3)
>>>
``````

``````>> img[:,:,0]
array([[255, 255, 255],
[124, 245, 255],
[135, 245, 255]])
>>>
``````

``````>>> img[...,0]
array([[255, 255, 255],
[124, 245, 255],
[135, 245, 255]])
>>>
``````

``````>>> img[1,...,1]
array([255, 222, 232])
>>>
``````