[英]Sorting a dictionary with values as tuple
I have a dictionary as follows and I want to sort this based on the last element of the tuple in a non-increasing manner.我有一个字典如下,我想根据元组的最后一个元素以非递增的方式对其进行排序。 However, if two or more of the last elements are the same, then they should be sorted based on the second element.
但是,如果最后两个或多个元素相同,则应根据第二个元素对它们进行排序。
capacity= {
'one': (57000, 3100, 0.12903225806451613),
'two': (52911, 2400, 0.125),
'three': (48200, 2400, 0.125),
'four': (48200, 2480, 0.125),
'five': (11464, 1200, 0.20833333333333334),
'six': (44000, 1172, 0.21331058020477817),
'seven': (42000, 1172, 0.21331058020477817)}
After sorting by the third element of the tuple I get the following results.按元组的第三个元素排序后,我得到以下结果。
sorted_capacity={k: (v1,v2,v3) for k, (v1,v2,v3) in sorted(capacity.items(), key=lambda x: x[1][2],reverse=True)}
sorted_capacity
{'six': (44000, 1172, 0.21331058020477817),
'seven': (42000, 1172, 0.21331058020477817),
'five': (11464, 1200, 0.20833333333333334),
'one': (57000, 3100, 0.12903225806451613),
'two': (52911, 2400, 0.125),
'three': (48200, 2400, 0.125),
'four': (48200, 2480, 0.125)}
However, as I mentioned before, I need to have 'four': (48200, 2480, 0.125)}
before 'two': (52911, 2400, 0.125), because 2480 is higher than 2400
.但是,正如我之前提到的,我需要在 'two': (52911, 2400, 0.125) 之前有
'four': (48200, 2480, 0.125)}
'two': (52911, 2400, 0.125), because 2480 is higher than 2400
。 Basically my output should be like this:基本上我的 output 应该是这样的:
sorted_capacity
{'six': (44000, 1172, 0.21331058020477817),
'seven': (42000, 1172, 0.21331058020477817),
'five': (11464, 1200, 0.20833333333333334),
'one': (57000, 3100, 0.12903225806451613),
'four': (48200, 2480, 0.125),
'two': (52911, 2400, 0.125),
'three': (48200, 2400, 0.125)
}
Can anyone help me figure out how to achieve this?谁能帮我弄清楚如何实现这一目标?
In the key function, you can reverse the tuples to compare:在密钥 function 中,可以反转元组进行比较:
>>> dict(sorted(capacity.items(), key=lambda item: item[1][::-1], reverse=True))
{'six': (44000, 1172, 0.21331058020477817),
'seven': (42000, 1172, 0.21331058020477817),
'five': (11464, 1200, 0.20833333333333334),
'one': (57000, 3100, 0.12903225806451613),
'four': (48200, 2480, 0.125),
'two': (52911, 2400, 0.125),
'three': (48200, 2400, 0.125)}
Your code with the issue fixed:您修复了问题的代码:
from pprint import pprint
capacity = {
'one': (57000, 3100, 0.12903225806451613),
'two': (52911, 2400, 0.125),
'three': (48200, 2400, 0.125),
'four': (48200, 2480, 0.125),
'five': (11464, 1200, 0.20833333333333334),
'six': (44000, 1172, 0.21331058020477817),
'seven': (42000, 1172, 0.21331058020477817)}
sorted_capacity = {
k: (v1, v2, v3)
for k, (v1, v2, v3) in sorted(capacity.items(), key=lambda x: (x[1][2], x[1][1]), reverse=True)
}
pprint(sorted_capacity, sort_dicts=False) # don't want pprint to sort again
This just adds the middle column to the sorting key
.这只是将中间列添加到排序
key
。 If you want the first column to be used when the second is also identical, you could also just reverse the tuple:如果您希望在第二列也相同时使用第一列,您也可以反转元组:
key=lambda x: tuple(reversed(x[1]))
Or:或者:
key=lambda x: x[1][::-1]
Use利用
key = lambda x: (x[1][-1], x[1][1])
This compares tuples of the last and second elements in each tuple.这将比较每个元组中最后一个和第二个元素的元组。 Since tuples are ordered lexicographically, this produces the ordering you want.
由于元组是按字典顺序排序的,因此这会产生您想要的排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.