gpt4 book ai didi

python - 为什么打印 my_list[10 :0:-1] stop with the second item rather than the first item in Python?

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:14 28 4
gpt4 key购买 nike

my_list = range(1,11)
print my_list[10:0:-1]

这段代码打印出 [10, 9, 8, 7, 6, 5, 4, 3, 2],这是没有意义的。为什么要这样做?

最佳答案

因为切片中的第二个元素是独占的。索引从 10 到 1,而不是 0。

要反转my_list,只需省略开始和结束:

print my_list[::-1]

关于python - 为什么打印 my_list[10 :0:-1] stop with the second item rather than the first item in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15161709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com