1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#(1)迭代字典
d = {'x':1, 'y':2, 'z':3}
##迭代字典的键
for key in d:
print(key, 'value is', d[key])
##迭代字典的每一项
for key, value in d.items():
print(key, 'value is', value)
#(2) zip()可缝合多个等长序列,用于迭代
name=['AA',"BB","CC"]
age=[12,15,11]
list(zip(name, age))
## [('AA', 12), ('BB', 15), ('CC', 11)]
for name,age in zip(name, age):
print(name, 'is', age, 'years old')
#(3) enumerate()同时迭代索引
strs = ['A', 'B', 'C']
for index, st in enumerate(strs):
print(index, st)
## 0 A
## 1 B
## 2 C
|