参考链接: class slice(stop)
参考链接: slice – 切片
参考链接: Ellipsis
代码实验:
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.
>>> ...
Ellipsis
>>> print(...)
Ellipsis
>>>
>>> Ellipsis
Ellipsis
>>> print(Ellipsis)
Ellipsis
>>> ellipsis
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
ellipsis
NameError: name 'ellipsis' is not defined
>>> Ellipsis is ...
True
>>> Ellipsis == ...
True
>>>
>>> import numpy as np
>>> a = np.arange(81).reshape(3,3,3,3)
>>> 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, 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]]]])
>>>
>>> a[1,...,1]
array([[28, 31, 34],
[37, 40, 43],
[46, 49, 52]])
>>>
>>> a[1,Ellipsis,1]
array([[28, 31, 34],
[37, 40, 43],
[46, 49, 52]])
>>>
>>>
>>>
slice对象,请看代码,他的作用就是切片,即类似于[ : : ]的语法:
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.
>>> alphabets = 'abcdefghijklmnopqrstuvwxyz'
>>> index = slice(0,25,3)
>>> index
slice(0, 25, 3)
>>> alphabets[index]
'adgjmpsvy'
>>> alphabets[0:25:3]
'adgjmpsvy'
>>> ls = list(range(30))
>>> ls
[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]
>>> ls[index]
[0, 3, 6, 9, 12, 15, 18, 21, 24]
>>> ls[0:25:3]
[0, 3, 6, 9, 12, 15, 18, 21, 24]
>>>
>>>