索引在python中意味着什么?

时间:2021-05-19 16:56:44

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和结束索引结束。

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.

4 个解决方案

#1


6  

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 find only from the second to second-to-last characters, you can do:

大多数情况下,您可以*地混合正负索引。因此,例如,如果您只想使用从第二个到倒数第二个字符的查找,您可以执行以下操作:

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

#2


6  

"index" is meant as "position".

“index”是指“位置”。

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 beg and will end its search at the end index called end. So it will only search between beg and end. Usually (by default) beg is 0 (which means it is the first character in the string) and end is 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为0(表示它是字符串中的第一个字符),end是字符串的长度减去1(这意味着它是字符串中的最后一个字符)。所以索引只是一个位置(不仅在一个字符串中,例如也在一个数组中)。

#3


4  

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

考虑这个字符串“Hello”。如果你想指出一些像e这样的字符你需要一个索引,这是一个位置编号。 python中的索引从零开始计数。所以“Hello”中的字母e的索引是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”中的内容。

#4


3  

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)中的内置序列类型是字符串(unicode字符序列),字节(从0到255的小整数序列,即字节值的序列),元组(任何元素的序列)和列表(序列)任何元素)。所有这些都是通过数组实现的;通过这种方式,它们可以像任何(其他)编程语言中的数组一样被编入索引。

If a is 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]指的是街道中第一个房屋的内容,[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] - 即。引用 - 可用于更改目标对象内容...如果目标对象是可变的。

#1


6  

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 find only from the second to second-to-last characters, you can do:

大多数情况下,您可以*地混合正负索引。因此,例如,如果您只想使用从第二个到倒数第二个字符的查找,您可以执行以下操作:

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

#2


6  

"index" is meant as "position".

“index”是指“位置”。

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 beg and will end its search at the end index called end. So it will only search between beg and end. Usually (by default) beg is 0 (which means it is the first character in the string) and end is 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为0(表示它是字符串中的第一个字符),end是字符串的长度减去1(这意味着它是字符串中的最后一个字符)。所以索引只是一个位置(不仅在一个字符串中,例如也在一个数组中)。

#3


4  

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

考虑这个字符串“Hello”。如果你想指出一些像e这样的字符你需要一个索引,这是一个位置编号。 python中的索引从零开始计数。所以“Hello”中的字母e的索引是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”中的内容。

#4


3  

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)中的内置序列类型是字符串(unicode字符序列),字节(从0到255的小整数序列,即字节值的序列),元组(任何元素的序列)和列表(序列)任何元素)。所有这些都是通过数组实现的;通过这种方式,它们可以像任何(其他)编程语言中的数组一样被编入索引。

If a is 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]指的是街道中第一个房屋的内容,[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] - 即。引用 - 可用于更改目标对象内容...如果目标对象是可变的。