num_dict = {‘a‘: 16, ‘b‘: 10, ‘c‘: 8, ‘d‘: 0}
#方法1
# new_dict = sorted(num_dict.items(),key=lambda x:x[1])
# print(dict(new_dict))
#方法2
def new_dict(a_dict):
b_dict = {} #用于存放排序后的字典
values = sorted(a_dict.values()) #把16,10,8,0进行排序
print(values) #打印出 [0, 8, 10, 16]
#接下来要匹配key值
for i in values:
for k,v in num_dict.items(): #所有的数据项取出来
if v==i:
b_dict[k] = v #添加数据项到b_dict字典里
print(b_dict)
new_dict(num_dict)