我需要按年龄对以下字典进行排序,但我不知道如何:
1 {'name':'ste', 'surname':'pnt', 'age':21}
2 {'name':'test', 'surname':'black','age':24}
3 {'name':'check', 'surname':'try', 'age':41}
这是for
周期的输出:
for k, v in d.items():
print(k, v)
答案 0 :(得分:1)
d = {
1 : {'name':'ste', 'surname':'pnt', 'age':21},
2 : {'name':'test', 'surname':'black','age':24},
3 : {'name':'check', 'surname':'try', 'age':41},
}
sorted(d.values(),key=lambda it: it['age'])
答案 1 :(得分:-2)
所以你的字典已按年龄排序,但是......
for key in sorted(mydict.iterkeys()):
print "%s: %s" % (key, mydict[key])
https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/