我們知道元組是 Python 中存在的數據結構,其中不同數據類型的元素可以包含在括弧中。
在本教程中,我們將學習如何在 Python 中反轉元組。
考慮以下例子來理解我們的目標-
Input - (21, 54, 1, 'apple', 'fruits', 11.7)
Output - (11.7, 'fruits', 'apple', 1, 54, 21)
解釋-
我們可以在這裡觀察到,因為我們已經反轉了元組的元素,所以最後出現的浮點值排在第一位。
讓我們再看一個例子-
Input - (20, 30, 50, 19.3, 41, 'comic books')
Output - ('comic books', 41, 19.3, 50, 30, 20)
解釋-
我們可以在這裡觀察到,因為我們已經反轉了元組的元素,所以最後出現的字元串值排在第一位。
我們將使用下面的方法來反轉 Python 中的元組,
- 使用切片
- 使用反向()方法
- 在 Python 中使用生成器
- 使用索引技術
那麼,讓我們從第一個開始:
使用切片
Python 中用於對特定範圍內的元素進行切片的切片技術。
下面的程序說明了如何使用它們。
#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#using slicing
tuple_values = tuple_values[::-1]
#displaying the tuple
print("The reversed tuple is: ", tuple_values)
輸出:
The original tuple is: (1, 2, 'Python', 'Java', 23.4, 77, 10)
The reversed tuple is: (10, 77, 23.4, 'Java', 'Python', 2, 1)
解釋-
是時候看看上面程序的解釋了-
- 在第一步中,我們已經用不同的值初始化了元組。
- 在此之後,我們顯示了我們的元組,然後通過將步驟指定為-1,使用切片以相反的順序列印元組的元素。
- 最後,我們顯示了反向元組。
在第二個程序中,我們將學習如何使用方法。
使用反向()方法
考慮下面給出的程序,
#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#using slicing
tuple_values = tuple(reversed(tuple_values))
#displaying the tuple
print("The reversed tuple is: ", tuple_values)
輸出:
The original tuple is: (1, 2, 'Python', 'Java', 23.4, 77, 10)
The reversed tuple is: (10, 77, 23.4, 'Java', 'Python', 2, 1)
解釋-
讓我們了解一下在上面的程序中發生了什麼,
- 在第一步中,我們已經用不同的值初始化了元組。
- 在此之後,我們顯示了我們的元組,然後使用 reversed() 以相反的順序列印元組的元素。
- 最後,我們顯示了反向元組。
在第三個程序中,我們將看到生成器如何用於同樣的目的。
在 Python 中使用生成器
下面給出的程序演示了如何在我們的 Python 程序中使用生成器。
#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#creating a function
def reverse_tup(tuple_values):
for i in reversed(range(len(tuple_values))):
yield tuple_values[i]
#displaying the items in reverse order
for i in reverse_tup(tuple_values):
print("The element of a reversed tuple are: ", i)
輸出:
The original tuple is: (1, 2, 'Python', 'Java', 23.4, 77, 10)
The elements of a reversed tuple are: 10
The elements of a reversed tuple are: 77
The elements of a reversed tuple are: 23.4
The elements of a reversed tuple are: Java
The elements of a reversed tuple are: Python
The elements of a reversed tuple are: 2
The elements of a reversed tuple are: 1
解釋-
是時候看一看解釋了,
- 在第一步中,我們已經用不同的值初始化了元組。
- 在這之後,我們已經顯示了我們的元組,然後創建了一個函數,該函數將元組作為其參數,並使用生成器的概念幫助我們以相反的順序獲得元組。
- 最後,我們顯示了反向元組。
最後,我們將了解索引如何幫助我們實現目標。
使用索引技術
下面的程序說明了如何做到這一點
#initializing the tuple
tuple_values=(1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#displaying the items in reverse order
for i in range(len(tuple_values)):
print("The element of a reversed tuple is: ", tuple_values[-(i+1)])
輸出:
The original tuple is: (1, 2, 'Python', 'Java', 23.4, 77, 10)
The element of a reversed tuple is: 10
The element of a reversed tuple is: 77
The element of a reversed tuple is: 23.4
The element of a reversed tuple is: Java
The element of a reversed tuple is: Python
The element of a reversed tuple is: 2
The element of a reversed tuple is: 1
解釋-
是時候看看上面程序的解釋了-
- 在第一步中,我們已經用不同的值初始化了元組。
- 在此之後,我們顯示了我們的元組,然後創建了一個函數,該函數接受元組的元素,使用
for
循環遍曆元組,然後通過將索引指定為-1 以相反的順序列印元素。 - 最後,我們顯示了反向元組。
結論
在本教程中,我們討論了在 Python 中反轉元組的不同方法。
原創文章,作者:LNVZP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330230.html