如何基于键对字典进行排序,同时还在python中打印出值

时间:2015-10-19 14:37:46

标签: python sorting dictionary

这是我的字典

x = {'balance': 19.1, 'advance': 0.4, 'withdrawal': 53.0, 'deposit': 24.7, 'transfer': 2.8000000000000003}

当我尝试按

排序时
sorted(x) = ['advance', 'balance', 'deposit', 'transfer', 'withdrawal']

它只给了我钥匙,我还需要相应的值

1 个答案:

答案 0 :(得分:0)

x = {'balance': 19.1, 'advance': 0.4, 'withdrawal': 53.0, 'deposit': 24.7, 'transfer': 2.8000000000000003}
print [i for i in sorted(x.items())]

输出:[('advance', 0.4), ('balance', 19.1), ('deposit', 24.7), ('transfer', 2.8000000000000003), ('withdrawal', 53.0)]