【发布时间】:2021-07-16 03:29:43
【问题描述】:
我有一个字典列表,如下所示,
x = [
{"matches_count": 10, distance: 50},
{"matches_count": 20, distance: 50},
{"matches_count": 1, "distance: 20},
{"matches_count": 2, "distance": 20},
{"matches_count": 5, "distance: 30},
]
我需要像下面这样排序
x = [
{"matches_count": 2, distance: 20},
{"matches_count": 1, "distance: 20},
{"matches_count": 5, "distance: 30},
{"matches_count": 20, distance: 50},
{"matches_count": 10, "distance": 50}
]
基本上,这应该使用 distance 和 matches_count 进行排序。当距离相同时,最高匹配数应该排在第一位。有没有可能用 sorted 方法做到这一点?或者有没有其他方法可以做到这一点?
我已经检查了以下问题,但我无法从这些问题中获得正确的见解。 Sort list of dictionaries by multiple keys with different ordering
【问题讨论】:
标签: python