为什么python返回第n+1个列表元素而不是第n个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25805239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:40:35  来源:igfitidea点击:

Why does python return the n+1th list element rather than the nth?

pythonpython-3.x

提问by useR

i m new to Python and have following problem

我是 Python 新手并且有以下问题

>>> choice = [1,0,1,1,]
>>> choice = [1,0,1,1]
>>> print(choice)
[1, 0, 1, 1]
>>> print(choice[2])
1

why it print 1 rather than 0?

为什么它打印 1 而不是 0?

采纳答案by cribalik

Python uses something that is called zero based indexing, which means that the first element in a list is referred to element number 0and not 1.

Python 使用一种称为基于零的索引,这意味着列表中的第一个元素被引用为元素编号0而不是1

回答by NPE

It prints 1 because list indices start from zero and not from one. Thus:

它打印 1,因为列表索引从零开始而不是从 1 开始。因此:

choice[0] is  1
choice[1] is  0
choice[2] is  1
choice[3] is  1

回答by Papouche Guinslyzinho

Because of the way math works, Python starts its lists at 0 rather than 1. It seems weird, but there are many advantagesto this, even though it is mostly arbitrary.

由于数学的工作方式,Python 从 0 而不是 1 开始其列表。这看起来很奇怪,但这样做有很多优点,尽管它大多是任意的。