迭代(Iteration)
给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple
在Python中,迭代是通过for … in来完成的
dict迭代
d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
print(key)
因为dict的存储不是按照list的方式顺序排列,所以,迭代出的结果顺序很可能不一样
C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
a
b
c
Process finished with exit code 0
dict迭代的是key。如果要迭代value,可以用for value in d.values()
d = {'a': 1, 'b': 2, 'c': 3}
for value in d.values():
print(value)
C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
1
2
3
Process finished with exit code 0