首頁  >  文章  >  後端開發  >  正數和負數索引在Python中是什麼意思?

正數和負數索引在Python中是什麼意思?

WBOY
WBOY轉載
2023-08-20 19:37:071629瀏覽

正數和負數索引在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 indexing (-). 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刪除