参考链接: Structural indexing tools
参考链接: np.clip() 函数 和 numpy中切片省略号的用法
参考链接: NumPy 切片和索引
参考链接: Indexing, Slicing and Iterating
NumPy中的省略号语法:
实验展示:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> z = np.arange(81).reshape(3,3,3,3)
>>> z
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],
[30, 31, 32],
[33, 34, 35]],
[[36, 37, 38],
[39, 40, 41],
[42, 43, 44]],
[[45, 46, 47],
[48, 49, 50],
[51, 52, 53]]],
[[[54, 55, 56],
[57, 58, 59],
[60, 61, 62]],
[[63, 64, 65],
[66, 67, 68],
[69, 70, 71]],
[[72, 73, 74],
[75, 76, 77],
[78, 79, 80]]]])
>>> 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]])
>>> z[...]
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],
[30, 31, 32],
[33, 34, 35]],
[[36, 37, 38],
[39, 40, 41],
[42, 43, 44]],
[[45, 46, 47],
[48, 49, 50],
[51, 52, 53]]],
[[[54, 55, 56],
[57, 58, 59],
[60, 61, 62]],
[[63, 64, 65],
[66, 67, 68],
[69, 70, 71]],
[[72, 73, 74],
[75, 76, 77],
[78, 79, 80]]]])
>>> z[,...,]
SyntaxError: invalid syntax
>>>
>>>
>>> z[,...]
SyntaxError: invalid syntax
>>> z[...,]
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],
[30, 31, 32],
[33, 34, 35]],
[[36, 37, 38],
[39, 40, 41],
[42, 43, 44]],
[[45, 46, 47],
[48, 49, 50],
[51, 52, 53]]],
[[[54, 55, 56],
[57, 58, 59],
[60, 61, 62]],
[[63, 64, 65],
[66, 67, 68],
[69, 70, 71]],
[[72, 73, 74],
[75, 76, 77],
[78, 79, 80]]]])
>>>
>>>
>>>
>>>
>>>
>>> ls = [
[0,1,2,3],
[4,5,6,7],
[8,9,10,11]
]
>>> ls
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
>>> ls[...,0]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
ls[...,0]
TypeError: list indices must be integers or slices, not tuple
>>> ls[...]
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
ls[...]
TypeError: list indices must be integers or slices, not ellipsis
>>> x = np.array(ls)
>>> x
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[...,]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[...,0]
array([0, 4, 8])
>>> x[:,0]
array([0, 4, 8])
>>>
>>>
>>>
>>> z
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],
[30, 31, 32],
[33, 34, 35]],
[[36, 37, 38],
[39, 40, 41],
[42, 43, 44]],
[[45, 46, 47],
[48, 49, 50],
[51, 52, 53]]],
[[[54, 55, 56],
[57, 58, 59],
[60, 61, 62]],
[[63, 64, 65],
[66, 67, 68],
[69, 70, 71]],
[[72, 73, 74],
[75, 76, 77],
[78, 79, 80]]]])
>>> z[0,..,1]
SyntaxError: invalid syntax
>>> z[0,...,1]
array([[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]])
>>> z[0,:,:,1]
array([[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]])
>>>
>>>
>>>
>>> z[0,...]
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]]])
>>> z[0,:,:,:]
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]]])
>>> z[:,:,:,1]
array([[[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]],
[[28, 31, 34],
[37, 40, 43],
[46, 49, 52]],
[[55, 58, 61],
[64, 67, 70],
[73, 76, 79]]])
>>> z[...,1]
array([[[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]],
[[28, 31, 34],
[37, 40, 43],
[46, 49, 52]],
[[55, 58, 61],
[64, 67, 70],
[73, 76, 79]]])
>>>
>>>
总结:
NumPy数组切片可以使用省略号(...),
注意是英文省略号不是中文全角半角省略号,
省略号的作用是在NumPy多维数组指定某些维度后在剩余的维度采取全选操作.
可以只指定前面的维度也可以只指定后面的维度,
也可以既指定前面的维度同时又指定后面的维度,
余下的维度使用省略号进行全选.
注意: python的内置类型列表是不支持省略号全选操作的.