三种常见的字典遍历实现
def get_key_value(dics):
'''
遍历所有键值对
'''
for key, value in dics.items():
print(f"{key}: {value}")
def get_keys(dics):
'''
遍历所有的键
'''
for key in dics.keys():
print(key)
def get_values(dics):
'''
遍历所有的值
'''
for value in dics.values():
print(value)
if __name__ == "__main__":
dics = {
'name': 'xiaopangyu',
'age': 18,
'sex': 'man',
'high': 180
}
# 三种字典遍历方法
print("========== 遍历所有的key value ==========")
get_key_value(dics)
print("========== 遍历所有的key ==========")
get_keys(dics)
print("========== 遍历所有的value ==========")
get_values(dics)