首頁  >  文章  >  後端開發  >  掌握 Python 清單:您需要了解的基本技術

掌握 Python 清單:您需要了解的基本技術

Patricia Arquette
Patricia Arquette原創
2024-11-12 22:28:02192瀏覽

Mastering Python Lists: Essential Techniques You Need to Know

為了

簡單的

這將循環遍歷列表,並且列表中的每個元素在每次迭代中都可以作為變數使用。當需要遍歷清單中的所有元素時,這被廣泛使用。

operating_systems = ["windows", "mac", "linux"]
for os in operating_systems:
    print(os)`

# Output
windows
mac
linux

對於和範圍

當需要根據索引存取並且需要索引值時。

operating_systems = ["windows", "mac", "linux"]
for i in range(len(operating_systems)):
    print(f"Index {i}: {operating_systems[i]}")
# Output
Index 0: windows
Index 1: mac
Index 2: linux

並枚舉

如果您同時需要索引和值,這是一種優雅的方式

operating_systems = ["windows", "mac", "linux"]
for index, os in enumerate(operating_systems):
    print(f"Index is {index} and value is {os}")
# Output
Index is 0 and value is windows
Index is 1 and value is mac
Index is 2 and value is linux

儘管

簡單的同時

operating_systems = ["windows", "mac", "linux"]
i = 0 # Inital condition, required to start
while i < len(operating_systems):
    print(f"While looping {i} got the value {operating_systems[i]}")
    i = i + 1 # This is very important, dont forget about infinite loops
# Output
While looping 0 got the value windows
While looping 1 got the value mac
While looping 2 got the value linux

迭代器

可以很好地控制何時向前移動迭代器,儘管我們必須依靠 StopIteration 來檢查是否到達末端。

operating_systems = ["windows", "mac", "linux"]
iterator = iter(operating_systems)
while True:
    try:
        os = next(iterator)
        print(f"Consumed form iterator {os}")
    except StopIteration:
        print("Consumed all from iterator")
        break
# Output
Consumed form iterator windows
Consumed form iterator mac
Consumed form iterator linux
Consumed all from iterator
# Hack to avoid StopIteration
iterator = iter(operating_systems)
end_of_list = object()
reached_end = False
while not reached_end:
    os = next(iterator, end_of_list)# a predefined object as end of the list
    if os != end_of_list:
        print(os)
    else:
        reached_end = True

列表理解

需要轉換時

operating_systems = ["windows", "mac", "linux"]
os_uppercase = [os.upper() for os in operating_systems]
print(os_uppercase) 
# Output
['WINDOWS', 'MAC', 'LINUX']

騎自行車

當需要循環列表時。使用適當的邊界條件來打破循環

import itertools
operating_systems = ["windows", "mac", "linux"]
for item in itertools.cycle(operating_systems):  
    print(item)
# Infinite cycling loopmake sure to have proper boundary condition to break
# Output
windows
mac
linux
windows
mac
linux
windows
mac
linux
windows
mac
linux
windows ....... Infinite loop

多個列表

同時循環多個列表。如果清單大小不同,請注意輸出。

operating_systems = ["windows", "mac", "linux"]
mobile_operating_systems = ["android", "ios"]

for os, mobile_os in zip(operating_systems,mobile_operating_systems):
    print(os, mobile_os)
# Output
windows android
mac ios

反向循環

operating_systems = ["windows", "mac", "linux"]
for reversed_os in reversed(operating_systems):
    print(reversed_os)
# Output
linux
mac
windows

以上是掌握 Python 清單:您需要了解的基本技術的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn