目录
- 少废话,直接上例子
- 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。
原文地址:https://www.cnblogs.com/yorkyu/p/10258922.html