python字典的四种遍历方式
-
使用for循环遍历字典的键:
my_dict = {'a': 1, 'b': 2, 'c': 3} for key in my_dict: print(key, my_dict[key])
-
使用items()方法遍历字典的键值对:
my_dict = {'a': 1, 'b': 2, 'c': 3} for key, value in my_dict.items(): print(key, value)
-
使用keys()方法遍历字典的键:
my_dict = {'a': 1, 'b': 2, 'c': 3} for key in my_dict.keys(): print(key, my_dict[key])
-
使用values()方法遍历字典的值:
my_dict = {'a': 1, 'b': 2, 'c': 3} for value in my_dict.values(): print(value)
###################################
标签:遍历,python,value,dict,key,my,字典 From: https://www.cnblogs.com/herd/p/18297372