如何使用值对具有多个键并且每个键的值都是一个字典的字典进行排序?

huangapple go评论68阅读模式
英文:

I have a dictionary defined with multiple keys and the value for each key is a dictionary himself, how can I sort using values?

问题

以下是翻译好的部分:

程序的结果如下所示:

{'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}, 
 'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
 'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}
}

我想按照"PF"的值来排序输出,"PF"值越高,排名越靠前。如果多个队伍的"PF"值相等(在示例中我取消了一些队伍),则根据"Q"的值进行进一步排序("Q"值越高,排名越靠前)。

我是Python的新手,能有人帮助我吗?

结果应该如下所示:

Team7                      4    10    1.07     64    60
Team2                      3     9    1.51     53    35
Team1                      3     9    1.09     50    46
Team4                      4     8   0.935     58    62
Team5                      3     7     1.1     44    40
Team3                      3     7     1.0     44    44
Team8                      4     7   0.955     64    67
Team6                      4     6   0.964     54    56

希望这可以帮助你。

英文:

The results of my program is like this:

{'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}, 
 'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
 'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}
}

I want to sort the output like a standing using "PF", the more u have the upper u are in the standing, if "PF" is equals for multiple teams (i cancelled some teams in the example), the more criteria becomes "Q" (the more the upper).
I'm new to python, can someone help me please?

The results should be like that

Team7                      4    10    1.07     64    60
Team2                      3     9    1.51     53    35
Team1                      3     9    1.09     50    46
Team4                      4     8   0.935     58    62
Team5                      3     7     1.1     44    40
Team3                      3     7     1.0     44    44
Team8                      4     7   0.955     64    67
Team6                      4     6   0.964     54    56

答案1

得分: 2

不使用pandas或数据框架,如果指定正确的键,sorted 可以使这种排序非常简单。dict.items 函数将为您提供每个键和值的元组迭代器。该迭代器可以传递给 sorted 以生成排序列表。

d = {
  'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}, 
  'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
  'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}
}
sorted(d.items(), key=lambda x: x[1]['PF'])
[('Team2', {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}), 
 ('Team3', {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}), 
 ('Team1', {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70})]
[a for a, _ in sorted(d.items(), key=lambda x: x[1]['PF'])]
['Team2', 'Team3', 'Team1']

以上仅基于 PF 键进行排序,但如果我们希望在这些键相等的情况下按 Q 进行排序,我们可以创建这两个值的元组并基于该键进行排序。

这种方法可以扩展到按顺序对任意数量的字段进行排序。如果您希望按降序对字段进行排序,只需对其取反。例如,key=lambda x: (x[1]['PF'], -x[1]['Q'])

sorted(d.items(), key=lambda x: (x[1]['PF'], x[1]['Q']))
[('Team2', {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}), 
 ('Team3', {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}), 
 ('Team1', {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70})]

如果愿意,我们可以将其转回为字典。请注意,仅从Python 3.6版本开始,标准Python字典会保留插入顺序。

dict(sorted(d.items(), key=lambda x: (x[1]['PF'], x[1]['Q'])))
{'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
 'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}, 
 'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}}
英文:

Without using pandas or dataframes, sorted makes this type of thing very simple if you specify the right key. The dict.items function will give you an iterator over tuples of each key and value. That iterator can be passed to sorted to generate a sorted list.

>>> d = {
...   'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}, 
...   'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
...   'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}
... }
>>> sorted(d.items(), key=lambda x: x[1]['PF'])
[('Team2', {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}), 
 ('Team3', {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}), 
 ('Team1', {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70})]
>>> [a for a, _ in sorted(d.items(), key=lambda x: x[1]['PF'])]
['Team2', 'Team3', 'Team1']

The above sorts purely based on the 'PF' key, but if we want to sort on 'Q' if those are equal, we can create a tuple of the two values and sort based on that key.

This approach can be extended to sort on any number of fields in order. If you wished to sort on a field in descending order, you need only negate it. E.g. key=lambda x: (x[1]['PF'], -x[1]['Q']).

>>> sorted(d.items(), key=lambda x: (x[1]['PF'], x[1]['Q'])))
[('Team2', {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}), 
 ('Team3', {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}), 
 ('Team1', {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70})]

We can, if we wish, convert that back to a dictionary. Note that only from version 3.6 onward do standard Python dictionaries retain insertion order.

>>> dict(sorted(d.items(), key=lambda x: (x[1]['PF'], x[1]['Q'])))
{'Team2': {'GIOCATE': 3, 'PUNTI': 7, 'Q': 1.1, 'PF': 44, 'PS': 40}, 
 'Team3': {'GIOCATE': 3, 'PUNTI': 9, 'Q': 1.514, 'PF': 53, 'PS': 35}, 
 'Team1': {'GIOCATE': 4, 'PUNTI': 6, 'Q': 0.9, 'PF': 63, 'PS': 70}}

huangapple
  • 本文由 发表于 2023年6月29日 03:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定