python里什么是切片_[::1]在python切片中是什么意思?

在Python中,您使用的方法称为Slicing。在

python中的切片语法如下:

[ : : ]

其中添加step部分是可选的。在

下面是pythonlist如何考虑正索引和负索引的表示。在+ -+ -+ -+ -+ -+ -+

| P | y | t | h | o | n |

+ -+ -+ -+ -+ -+ -+

0 1 2 3 4 5

-6 -5 -4 -3 -2 -1

使用时

^{pr2}$

为什么,因为当我们给0/-10作为第二个参数时,它排除了第0/-10位置的元素。在

所以简单的方法是在切片中省略第二个参数。也就是说a[10::-1] #or

a[-1::-1]

# Output = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# Which is what we want.

为了进一步简化,如果忽略起始值和结束值,只将步骤设为-1,则同样会产生相同的结果。在a[::-1]

# Output = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# The -1 at step position traverse the element from the last to the beginning

这里有一个简单的备忘单来理解切片a[start:end] # items start through end-1

a[start:] # items start through the rest of the array

a[:end] # items from the beginning through end-1

a[:] # a copy of the whole array

a[start:end:step] # start through not past end, by step

a[-1] # last item in the array

a[-2:] # last two items in the array

a[:-2] # everything except the last two items

要了解有关切片的更多信息,see this

希望这有帮助!:)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值