Python 确定字典列表是否具有等效值

Python 确定字典列表是否具有等效值,python,python-3.x,Python,Python 3.x,如何找出以下词典列表中的分数相同 sorted_list = [ { "team":"team", "score":2, "matches":3, "goal_difference":2 }, { "team":"team", "score":2, "matches":3, "goal_difference":3 }, { "team":"team",

如何找出以下词典列表中的分数相同

sorted_list = [
   {
      "team":"team",
      "score":2,
      "matches":3,
      "goal_difference":2
   },
   {
      "team":"team",
      "score":2,
      "matches":3,
      "goal_difference":3
   },
   {
      "team":"team",
      "score":1,
      "matches":3,
      "goal_difference":-1
   },
   {
      "team":"team",
      "score":4,
      "matches":3,
      "goal_difference":3
   }
]
例如,在上面的列表中,
sorted_list[0]。score==sorted_list[1]。score
。我将根据值对该列表进行不同的排序<代码>得分具有最高优先级,然后是
目标_差异
然后是
匹配

if list has equivalent score numbers:
    sorted_list = sorted(sorted_list , key=lambda k: k['goal_differences'])
else if list has equivalent goal_differences:
    sorted_list = sorted(sorted_list , key=lambda k: k['matches'])
else sorted_list = sorted(sorted_list , key=lambda k: k['score'])

您可以使用适当的按键功能:

from operator import itemgetter

sorted(sorted_list, key=itemgetter('score', 'goal_difference', 'matches'))
如果您需要一些特殊的逻辑,如先高分,再低匹配,请构建您自己的密钥:

sorted(sorted_list, key=lambda d: (-d['score'], -d['goal_difference'], d['matches']))

如何确定以下词典列表中的分数相同?

len(set([x["score"] for x in sorted_list])) == 1

非常感谢。你做得更好,没有条件:)。工作起来很有魅力!