首页  >  文章  >  后端开发  >  正数和负数索引在Python中是什么意思?

正数和负数索引在Python中是什么意思?

WBOY
WBOY转载
2023-08-20 19:37:071631浏览

正数和负数索引在Python中是什么意思?

通过索引,我们可以访问Python序列数据类型中的项目。字符串、列表、元组和范围对象都是序列数据类型。在本教程中,我们将详细介绍索引。

什么是列表索引?

任何编程语言中的线性数据结构都是围绕索引构建的。每台机器的默认索引从0开始,一直到n-1。在这种情况下,n表示相应数据结构中的总项目数。类型包括

  • Positive indexing − Increases from 0 to 1.

  • Negative indexing − each traversal moves from tail to head, starting with the last element.

These facilitate our ability to access the many components of this data structure. Let's examine the procedures in the next part.

Tuple Indexing

类似于我们如何访问列表和字符串中的元素,我们也可以访问元组中的元素。因此,索引和切片是我们访问元素所需的唯一方法。此外,索引是直观的,从索引零开始,就像列表一样。此外,我们在方括号中放置的数字表示元组的索引。让我们看一些使用元组索引来检索元组元素的示例。

Example 1

的中文翻译为:

示例1

tup1 = (10, 3, 4, 22, 1)
# for accessing the first element of the tuple
print(tup1[0])

Output

10

Example 2

tup1 = (10, 3, 4, 22, 1)
# accessing the third element of the tuple
print(tup1[2])

Output

4

Example 3

的中文翻译为:

示例 3

tup1 = (10, 3, 4, 22, 1)
print(tup1[10])
# gives an error as the index is only up to 4

Output

IndexError: tuple index out of range

Example 4

tup1 = (10, 3, 4, 22, 1)
print(tup1[1+3])
# the expression inside the square brackets results in an integer index 4. Hence, we get the element at the 4th index.

Output

1

Zero-Based Indexing in Python

In Python, positive zero-based indexing is the fundamental method for accessing iterable items.

As a result, an index starting at 0 may refer to any element in the iterable.

第一个以零为基准的索引元素的索引为0,第二个为1,依此类推。

Example 5

myList = [1, 2, 3, 4, 5]
# NORMAL INDEXING
print(myList[0])
print(myList[1]) 
print(myList[2])
# NEGATIVE INDEXING (STARTS FROM THE LAST ELEMENT IN THE LIST)
print(myList[-1])
print(myList[-2])
print(myList[-3])
print(myList[-3:])

Output

1
2
3
5
4
3
[3, 4, 5]

负索引

If we wish to print the components starting at the end, we may also use negative indexes. Additionally, by specifying the index number with a negative sign, we may carry out negative tuple indexing (-). As a result, this suggests that the compiler starts thinking about the elements in reverse order, beginning with the element that comes last in the tuple.

Example 3

的中文翻译为:

示例 3

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4])

Output

2

Now, we can use negative indexes in slicing also.

Example 4

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4:-1])

Output

(2, 56, 890)

Tuple index() 方法

The tuple index() function aids in locating an element's index or location within a tuple. Essentially, this function serves two purposes −

Giving the tuple's first instance of each element.

If the indicated element is absent from the tuple, throwing an error.

Syntax

tuple.index(value)

示例5:使用负索引删除元素

使用pop()函数并将-1作为参数传递给它,我们可以删除该列表的最后一个元素,并获得一个新列表。

my_list = [45, 5, 33, 1, -9, 8, 76]
my_list.pop(-1)
print(my_list)

Output

[45, 5, 33, 1, -9, 8]

Example 6: Finding the index of an element

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup.index(45))
print(tup.index(890))
#prints the index of elements 45 and 890

Output

2
7

在Python列表中使用负索引的优势

  • 需要更少的代码行数,并且可以在一行中完成反转。

  • Simplifies difficult processes.

  • 在要求很少复杂性的情况下快速运行

Conclusion

总之,Python允许从零开始进行正向索引和从-1开始进行负向索引。在Python中,负向索引表示索引过程从可迭代对象的末尾开始。最后一个元素位于索引-1,倒数第二个元素位于索引-2,依此类推。负向索引在Python计算机语言中支持数组,但在大多数其他编程语言中不支持。这意味着索引值-1提供了数组的最后一个元素,-2提供了倒数第二个元素。负向索引开始的地方是数组的末尾。

(2, 56, 890)

以上是正数和负数索引在Python中是什么意思?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除