[Python3] 007 列表的遍歷,你是 for 聯盟還是 while 部落
目錄
- 少廢話,直接上例子
- for 聯盟
- for 與 list 的簡單合作
- for 的老搭檔 range()
- for 與嵌套列表(雙層列表)
- for 從 if 那兒認識的 else
- 循環“三傑”
- while 部落
- for 寫了挺多,感覺有些對不住 while
- for 聯盟
少廢話,直接上例子
for 聯盟
for 與 list 的簡單合作
# 例1
list1 = [0, 1, 2, 3, 4]
for i in list1:
print(i)
- 運行結果
0
1
2
3
4
for 語句會按一定順序(可自定義)逐個訪問 in 後的數據,然後執行其子程序。
in 前的 i 可以改為有具體意義的變量名,甚至可以是 _。
註意不要漏打冒號。
# 例2
list2 = ["I am YorkFish."]
for i in list2:
print(i)
- 運行結果
I am YorkFish.
for 的老搭檔 range()
# 例3
for i in range(5):
print(i)
print(type(range(5)))
- 運行結果
0
1
2
3
4
<class ‘range‘>
range(5) 相當於 range(0, 5),和列表的分片有些像,“左閉右開”。
# 例4
for i in range(2, 6):
print(i)
- 運行結果
2
3
4
5
range() 中的區域可調。
# 例5
for i in range(2, 6, 2):
print(i)
- 運行結果
2
4
range() 可以設置增幅。
# 例6
for i in range(2, 6, -1):
print(i)
print("Over.")
- 運行結果
Over.
增幅設置成 -1,從結果看,程序被跳過了。
# 例7
for i in range(6, 2, -1):
print(i)
- 運行結果
6
5
4
3
for 與嵌套列表(雙層列表)
# 例8 # list8 為嵌套列表,也叫雙層列表 list8 = [["one", 1], ["two", 2], ["three", 3]] for i,j in list8: print(i, "---", j)
- 運行結果
one --- 1
two --- 2
three --- 3
# 例9.1
list9 = [["one", 1, "一"], ["two", 2], ["three", 3, 4]]
for i,j in list9:
print(i, "---", j)
- 運行結果
ValueError……too many values to unpack (expected 2)
數字格式錯誤(省略部分信息)需解包的值多於 2 個(我覺得這裏的 2 指 i, j)。
# 例9.2
list9 = [["one", 1, "一"], ["two", 2], ["three", 3, 4]]
for i,j,k in list9:
print(i, "---", j)
- 運行結果
ValueError……not enough values to unpack (expected 3, got 2)
值不足,應為 3 個,只得到 2 個(我覺得說的是 ["two", 2])。
# 例9.3 雙層列表的變異
list9 = [["one", 1, "一"], ["two", 2, "二"], ["three", 3, 4]]
for i,j,k in list9:
print(i, "---", j, "---", k)
- 運行結果
one --- 1 --- 一
two --- 2 --- 二
three --- 3 --- 4
# 例9.4 三重列表?
list9 = [ [["one"], [1], ["一"]], [["two"], [2], ["二"]] ]
for i,j,k in list9:
print(i, "---", j, "---", k)
- 運行結果
[‘one‘] --- [1] --- [‘一‘]
[‘two‘] --- [2] --- [‘二‘]
# 例9.5 三重列表?
list9 = [[["one1","one2"], [11, 12], ["一1", "一2"]], \ # 一行寫不下,加個反斜杠表示下行繼續
[["two1", "two2"], [21, 22], ["二1", "二2"]], [["three1", "three2"], [31, 32], ["三1", "三2"]]]
for i,j,k in list9:
print(i, "---", j, "---", k)
- 運行結果
[‘one1‘, ‘one2‘] --- [11, 12] --- [‘一1‘, ‘一2‘]
[‘two1‘, ‘two2‘] --- [21, 22] --- [‘二1‘, ‘二2‘]
[‘three1‘, ‘three2‘] --- [31, 32] --- [‘三1‘, ‘三2‘]
1 從結果看,這種方法仍然只能到第二層
2 我把 i,j,k 寫成 [i,j,k] 結果一樣
for 從 if 那兒認識的 else
# 例10
# 打印列表(名單)中的名字
# 如果列表中沒有想要的,或者列表遍歷結束,打印提示語
for name in [‘Batman‘, ‘Wonder Woman‘, ‘Aquaman‘, ‘Green Lantern‘, ‘Harley Quinn‘]:
print(name)
if name == "Harley Quinn":
print("{0}, when can I see your independent film?".format(name))
else:
print("I‘ve seen your movies already.")
else:
print("Green Lantern, I want to see your sequel!")
- 運行結果
Batman
I‘ve seen your movies already.
Wonder Woman
I‘ve seen your movies already.
Aquaman
I‘ve seen your movies already.
Green Lantern
I‘ve seen your movies already.
Harley Quinn
Harley Quinn, when can I see your independent film?
Green Lantern, I want to see your sequel!
循環“三傑”
- break 無條件結束所在層級的整個循環
- continue 無條件結束本次循環,進行所在層級的新一輪循環
- pass 像是闖關遊戲中的“過”,像是“擋拆”,更像是暫時性的“占著xx不xx”
# 例11 break
for i in range(3):
print("i = ", i)
for j in range(3):
print("j = ", j)
if j == 1:
print("Over.")
break
else:
print("Not found.")
- 運行結果
i = 0
j = 0
Not found.
j = 1
Over.
i = 1
j = 0
Not found.
j = 1
Over.
i = 2
j = 0
Not found.
j = 1
Over.
# 例12 continue
for i in range(3):
print("i = ", i)
for j in range(3):
print("j = ", j)
if j == 1:
print("Over.")
continue
else:
print("Not found.")
- 運行結果
i = 0
j = 0
Not found.
j = 1
Over.
j = 2
Not found.
i = 1
j = 0
Not found.
j = 1
Over.
j = 2
Not found.
i = 2
j = 0
Not found.
j = 1
Over.
j = 2
Not found.
# 例13 pass
for i in range(5):
pass # 先用 pass 占位,以免報錯,日後有好的 idea 可補上
print("Over.")
- 運行結果
Over.
while 部落
# 例14
list14 = [0, 1, 2, 3, 4]
length = len(list14)
index = 0
while index < length:
print(list14[index])
index += 1
- 運行結果
0
1
2
3
4
for 寫了挺多,感覺有些對不住 while
理論上 for 聯盟與 while 部落的實力不相上下,即 for 的代碼與 while 的代碼可互改。
個人感覺 Python 的設計使得 for 與 while 各有側重。
挖個坑,待我學有小成,聊聊 for 與 while 各自特定的用法,編號 Py007-1。
[Python3] 007 列表的遍歷,你是 for 聯盟還是 while 部落