如何获得按三个标准排序的词典列表

How to get a list of dictionaries sorted by three criteria

我想得到一个按照以下规则排序的列表 persons.sort(key = lambda p: (abs(p["height"] - 180), p["weight"]==75, p["weight"])) 即我需要它首先根据它与高度 180 的接近程度进行排序,然后是所有等于 75 的权重值,然后我希望它们按权重升序排列...

我想出了以下代码,但它不起作用...

def get_ordered_list(persons):
    persons.sort(key = lambda p: p["name"] )
    persons.sort(key = lambda p: (abs(p["height"] - 180), p["weight"]==75, p["weight"]))
    return persons

例如下面的示例数据,

array = [{"name": "Guido Batista", "height": 195, "weight": 110},
    {"name":"Heitor Tostes", "height": 180, "weight": 75},
    {"name":"Bruno Costa", "height": 180, "weight": 75},
    {"name":"Joao Kleber", "height": 180, "weight": 65},
    {"name":"Rafael Rodrigues", "height": 165, "weight": 110},
    {"name":"Ricardo Neto", "height": 170, "weight": 70},
    {"name":"Juca Carvalho", "height": 180, "weight": 77}]
    

我需要这样排序列表:

[
      {"name":"Bruno Costa", "height": 180, "weight": 75},
      {"name":"Heitor Tostes", "height": 180 , "weight": 75},
      {"name":"Joao Kleber", "height": 180, "weight": 65},
      {"name":"Juca Carvalho", "height": 180, "weight": 77},
      {"name":"Ricardo Neto", "height": 170, "weight": 70},
      {"name": "Guido Batista", "height": 195, "weight": 110},
      {"name":"Rafael Rodrigues", "height": 165, "weight": 110},
]

这将提供所需的结果,而不会进一步复杂化:

persons.sort(key = lambda p: (abs(p["height"] - 180), not(p["weight"]==75), p["weight"]))

默认情况下,排序顺序为升序。最后一块拼图是这个(来自doc

Booleans: These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively

按升序排列,0 在 1 之前,这就是上面发布的这一行可以解决问题的原因。

发生这种情况是因为 False 的值低于 True。使用

 persons.sort(key = lambda p: (abs(p["height"] - 180), p["weight"]!=75, p["weight"]))

更改“权重”键的比较。

尝试:

array = [
    {"name": "Guido Batista", "height": 195, "weight": 110},
    {"name": "Heitor Tostes", "height": 180, "weight": 75},
    {"name": "Bruno Costa", "height": 180, "weight": 75},
    {"name": "Joao Kleber", "height": 180, "weight": 65},
    {"name": "Rafael Rodrigues", "height": 165, "weight": 110},
    {"name": "Ricardo Neto", "height": 170, "weight": 70},
    {"name": "Juca Carvalho", "height": 180, "weight": 77},
]

array = sorted(
    array,
    key=lambda p: (
        abs(p["height"] - 180),
        p["weight"] != 75,
        p["name"],
        -p["weight"],
    ),
)
print(array)

打印:

[
    {"name": "Bruno Costa", "height": 180, "weight": 75},
    {"name": "Heitor Tostes", "height": 180, "weight": 75},
    {"name": "Joao Kleber", "height": 180, "weight": 65},
    {"name": "Juca Carvalho", "height": 180, "weight": 77},
    {"name": "Ricardo Neto", "height": 170, "weight": 70},
    {"name": "Guido Batista", "height": 195, "weight": 110},
    {"name": "Rafael Rodrigues", "height": 165, "weight": 110},
]