I want to print the keys and values with the values sorted from the largest to the smallest:
costumer_dict = {3:30, 1:22, 2:22}
sorted_values = [30, 22, 22]
sorted_dict = {}
for i in sorted_values:
for k in customer_dict.keys():
if customer_dict[k] == i:
sorted_dict[k] = customer_dict[k]
break
for x in list(sorted_dict)[:3]:
print(str(x) + ',' + str(sorted_dict[x]))
The answer I was expected is
3,30
1,22
2,22
But, it will only print out
3,30
1,22
But, if there are no duplicate values in the dictionary, it will print out the correct answer, so I'm wondering why this is happening. Can someone tell me where the code is wrong?