Rumah > Artikel > pembangunan bahagian belakang > Menguasai Senarai Python: Teknik Penting yang Anda Perlu Tahu
Ini akan melingkari senarai dan setiap elemen senarai akan tersedia sebagai pembolehubah dalam setiap lelaran. Ini digunakan secara meluas apabila terdapat keperluan untuk menyemak semua elemen senarai.
operating_systems = ["windows", "mac", "linux"] for os in operating_systems: print(os)`
# Output windows mac linux
Apabila terdapat keperluan untuk mengakses berdasarkan indeks dan nilai indeks diperlukan.
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
Ini adalah cara yang elegan, jika anda memerlukan kedua-dua indeks dan nilai
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
Memberi kawalan yang baik ke atas masa untuk menggerakkan iterator ke hadapan, walaupun kita perlu bergantung pada StopLelaran untuk menyemak sama ada penghujungnya telah dicapai.
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
Apabila transformasi diperlukan
operating_systems = ["windows", "mac", "linux"] os_uppercase = [os.upper() for os in operating_systems] print(os_uppercase)
# Output ['WINDOWS', 'MAC', 'LINUX']
Apabila berbasikal melalui senarai diperlukan. Gunakan dengan keadaan sempadan yang betul untuk memecahkan gelung
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
Gelung serentak pada berbilang senarai. Perhatikan output jika saiz senarai berbeza.
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
Atas ialah kandungan terperinci Menguasai Senarai Python: Teknik Penting yang Anda Perlu Tahu. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!