定义一个学生字典
stu = {"name": "zutuanxue_com", "age": 18, "sex": "男", "height": 173.5, "weight":80, "id": 1}
for key in stu
遍历字典的key
<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> stu:
print(key, stu[key])
for value in stu.values()
遍历字典元素
<span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> stu.values():
print(value)
for key, value in stu.items()
遍历字典的Key和Value
<span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> stu.items():
print(key, value)
for index, key in enumerate(stu)
枚举遍历,可以得到类似下标的顺序值,但是注意,字典是无序的
定义一个集合
s = set([1,2,3,4,5])
for key in s
for key in s:
print("--------", key)
for index, key in enumerate(s)
for index, key in enumerate(s):
print(index, key)