dict1 = {1: 3, 2: 500}
dict2 = {1: 5, 3: 4}
dict1_keys = list(dict1.keys())
dict2_keys = list(dict2.keys())
for key_2 in dict2_keys:
if key_2 in dict1_keys:
dict1[key_2] += dict2[key_2]
else:
dict1[key_2] = dict2[key_2]
output_dict = dict(sorted(dict1.items(), key=lambda x: x[1], reverse=True)) # x[1]表示按value来排, x[0]表示按keys来排,reverse=False表示从小到大排,
print(output_dict)
python 字典安装key或者value来进行排序
最新推荐文章于 2024-11-03 20:27:08 发布