索引在python中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20268195/
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 20:03:35  来源:igfitidea点击:

What does index mean in python?

pythonpython-3.x

提问by Rakanoth

Perhaps, it sounds like a stupid question. However, I have to learn what index is because I want to learn python.

也许,这听起来像是一个愚蠢的问题。但是,我必须学习索引是什么,因为我想学习python。

For example, in a website, I saw this:

例如,在一个网站上,我看到了这个:

The method find() determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given.

find() 方法确定字符串 str 是否出现在字符串中,或​​者如果给出了起始索引 beg 和结束索引 end ,则出现在字符串的子字符串中。

http://www.tutorialspoint.com/python/string_find.htm

http://www.tutorialspoint.com/python/string_find.htm

What does "index" mean here? I would like you to explain it to me like you are explaining something to a child. Because my comprehension is somewhat bad. Anyway. You can even provide examples to explain what index is.

这里的“索引”是什么意思?我希望你向我解释它,就像你在向孩子解释什么一样。因为我的理解力有些差。反正。您甚至可以提供示例来解释什么是索引。

Many thanks.

非常感谢。

采纳答案by Platinum Azure

An index, in your example, refers to a position within an ordered list. Python strings can be thought of as lists of characters; each character is given an index from zero (at the beginning) to the length minus one (at the end).

在您的示例中,索引是指有序列表中的位置。Python 字符串可以被认为是字符列表;每个字符都有一个索引,从零(开头)到长度减一(结尾)。

For the string "Python", the indexes break down like this:

对于字符串“Python”,索引分解如下:

P y t h o n
0 1 2 3 4 5

In addition, Python supports negative indexes, in which case it counts from the end. So the last character can be indexed with -1, the second to last with -2, etc.:

此外,Python 支持负索引,在这种情况下,它从末尾开始计数。所以最后一个字符可以用 索引-1,倒数第二个用-2,等等:

 P  y  t  h  o  n
-6 -5 -4 -3 -2 -1

Most of the time, you can freely mix positive and negative indexes. So for example, if you want to use findonly from the second to second-to-last characters, you can do:

大多数情况下,您可以自由混合正负索引。因此,例如,如果您只想使用find从倒数第二个到倒数第二个字符,您可以这样做:

"Python".find("y", beg=1, end=-2)

回答by Thomas Uhrig

"index" is meant as "position".

“索引”是指“位置”。

Let's use find()as an example: find()will look for a string in another string. It will start its search at the beginning index called begand will end its search at the end index called end. So it will only search between begand end. Usually (by default) begis 0 (which means it is the first character in the string) and endis the length of the string minus one (which means it is the very last character in the string). So an index is just a position (not only in a string, e.g. also in an array).

让我们find()作为一个例子:find()将在另一个字符串中查找一个字符串。它将在名为 的开始索引处开始搜索,beg并在名为 的结束索引处结束搜索end。所以它只会在beg和之间搜索end。通常(默认情况下)beg为 0(这意味着它是字符串中的第一个字符)并且end是字符串的长度减去 1(这意味着它是字符串中的最后一个字符)。所以索引只是一个位置(不仅在字符串中,例如也在数组中)。

回答by nio

Consider this string "Hello". If you wanted to point out to some of it's characters like eyou would need an index, which is a position number. Indices in python start counting from zero. So the index of letter ein "Hello"is 1.

考虑这个字符串"Hello"。如果你想指出它的一些字符,就像e你需要一个索引,它是一个位置编号。python 中的索引从零开始计数。所以字母ein的索引"Hello"是 1。

Try to run this line of code:

尝试运行这行代码:

print "Hello".find("e");

It should return you 1.

它应该回报你1

You can further play with it an run it again what it does. Try to replace "e"with "H", next try something that isn't in "Hello".

您可以进一步玩它并再次运行它。尝试替换"e""H",接下来尝试一些不在"Hello".

回答by pepr

If a street with houses is an analogy for an array with elements, then house number is analogy for index. However, Python uses numbers from zero.

如果有房子的街道类比有元素的数组,那么门牌号就是索引的类比。但是,Python 使用从零开始的数字。

The built in sequence types in Python (ver. 3) are strings (sequences of unicode characters), bytes (sequences of small integers from zero to 255, i.e. of byte values), tuples (sequences of whatever elements), and lists (sequences of whatever elements). All of them are implemented via arrays; this way they can be indexed as is usual for arrays in any (other) programming language.

Python(3任何元素)。它们都是通过数组实现的;通过这种方式,它们可以像任何(其他)编程语言中的数组一样被索引。

If ais such an array (i.e. one of the above sequence types), then a[0]refers to the content of the first house in the street, a[1]to the second house, etc. Some of the sequences can be modified (here only the lists), therefore they are called mutable. Some of the sequences cannot be modified (strings, bytes, tuples), therefore they are called immutable.

如果a是这样的数组(即上述序列类型之一),则a[0]引用街道第一房子的内容,a[1]第二房子的内容,等等。 一些序列可以修改(这里只列出列表),因此它们被称为可变的。一些序列不能被修改(字符串、字节、元组),因此它们被称为不可变的。

For mutable sequence type objects, the elements can be changed via assignment like lst[0] = 3, the elements of the immutable sequences can only be read and the value used, like print(s[3]).

对于可变序列类型的对象,元素可以通过赋值来改变,比如lst[0] = 3,不可变序列的元素只能读取和使用的值,比如print(s[3])

For lists and tuples the elements are not stored directly inside the array. Only the references to the target objects are stored inside. The target object is accessed indirectly (one hop along the reference). Think in terms that you go to the indexed house, and the person inside tells you where is the real content (in another house, not in this street). In such case, even the element from the (immutable) tuple like t[5]--i.e. the reference--can be used to change the target object content... if the target object is mutable.

对于列表和元组,元素不直接存储在数组中。只有对目标对象的引用存储在里面。目标对象被间接访问(沿引用一跳)。想想你去了索引的房子,里面的人告诉你真正的内容在哪里(在另一个房子里,而不是在这条街上)。在这种情况下,即使来自(不可变的)元组的元素(如t[5]引用)也可以用于更改目标对象的内容……如果目标对象是可变的。