【发布时间】:2020-07-24 06:57:27
【问题描述】:
例如我有这个字典
a = {'Sara': 3, 'Marie': 1, 'James': 1, 'Alex': 1} 我知道我可以用这样的代码一一打印(键,值):
for key,value in a.items():
print(key,value)
结果会是这样的:
Sara 3
Marie 1
James 1
Alex 1
我的问题是如何将此订单保留到:
Alex 1
James 1
Marie 1
Sara 3
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/questions/5455606/…
-
sorted(a.items(), key=lambda x : (x[1], x[0]))
标签: python python-3.x dictionary key key-value