1、三种常见的字典遍历实现
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)
2、打印结果
name: xiaopangyu
age: 18
sex: man
high: 180
========== 遍历所有的key ==========
name
age
sex
high
========== 遍历所有的value ==========
xiaopangyu
18
man
180
标签:遍历,get,python,value,---,dics,key,print From: https://www.cnblogs.com/xiaopangyu/p/18327719