搜尋
首頁後端開發Python教學如何在Python中以相反的順序迭代一個序列?

如何在Python中以相反的順序迭代一個序列?

Python 序列包括字串、列表、元組等。我們可以使用不同的方式合併 Python 序列的元素。讓我們來看看一些以相反順序迭代列表的範例。

使用while迴圈以逆序迭代

範例

在這個例子中,我們有一個作為序列的列表,並使用while循環以相反的順序進行迭代 -

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Length - 1
i = len(mylist) - 1

# Iterate in reverse order
print("Display the List in Reverse order = ")
while i >= 0 :
   print(mylist[i])
   i -= 1

輸出

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob

使用for迴圈以逆序迭代

範例

在這個例子中,我們有一個作為序列的列表,並使用for迴圈以相反的順序進行迭代-

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order
print("Display the List in Reverse order = ")
for i in range(len(mylist) - 1, -1, -1):
   print(mylist[i])

輸出

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob

使用reverse()逆序迭代

範例

在這個範例中,我們有一個List作為一個序列,並使用reversed()方法以相反的順序進行迭代 -

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order using reversed()
print("Display the List in Reverse order = ")
[print (i) for i in reversed(mylist)]

輸出

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob

使用負索引進行反向迭代

範例

在此範例中,我們將 List 作為序列,並使用負索引以相反順序迭代它 

#
# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order using negative indexing
print("Display the List in Reverse order = ")
[print (i) for i in mylist[::-1]]

輸出

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob

以上是如何在Python中以相反的順序迭代一個序列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能