Python内置常量Ellipsis和内置函数slice()

参考链接: 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]
>>> 
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值