Python:按值或键对字典进行排序并将结果子集到前三个键值对
Python: Sorting Dictionary By Value or Key And Subsetting Result to Top Three Key-Value Pairs
3 个问题:
- 有没有一种直接的方法可以让我在不导入外部包的情况下按 a)key 或 b)value 对字典进行降序排序?
- 如何简单地 select 仅按值在字典中排名前 3 的键值对?
- 字典按值排序,然后按键降序排列?
问题 1:
我在“output_dict_s1”下面有以下字典:
{'Sebastian Vettel': 0.6153846153846154, 'Lewis Hamilton': 0.8461538461538461, 'Kimi Raikkonen': 0.8181818181818182, 'Daniel Ricciardo': 0.25, 'Fernando Alonso': 0, 'Max Verstappen': 0.5, 'Nico Hulkenberg': 0, 'Valtteri Bottas': 0.5, 'Stoffel Vandoorne': 0, 'Carlos Sainz': 0, 'Pierre Gasly': 0, 'Kevin Magnussen': 0, 'Marcus Ericsson': 0, 'Esteban Ocon': 0, 'Sergio Perez': 0.14285714285714285, 'Charles Leclerc': 0, 'Lance Stroll': 0, 'Brendon Hartley': 0, 'Romain Grosjean': 0, 'Sergey Sirotkin': 0}
为了按值对字典进行排序,我使用了一个外部包。它可以工作,但我想知道如何在没有包的情况下完成它。
output_dict_sorted = dict(sorted(output_dict_s1.items(), key = operator.itemgetter(1), reverse = True))
问题 2:
现在排序后的字典“output_dict_sorted”看起来像这样:
{'Lewis Hamilton': 0.8461538461538461, 'Kimi Raikkonen': 0.8181818181818182, 'Sebastian Vettel': 0.6153846153846154, 'Max Verstappen': 0.5, 'Valtteri Bottas': 0.5, 'Daniel Ricciardo': 0.25, 'Sergio Perez': 0.14285714285714285, 'Fernando Alonso': 0, 'Nico Hulkenberg': 0, 'Stoffel Vandoorne': 0, 'Carlos Sainz': 0, 'Pierre Gasly': 0, 'Kevin Magnussen': 0, 'Marcus Ericsson': 0, 'Esteban Ocon': 0, 'Charles Leclerc': 0, 'Lance Stroll': 0, 'Brendon Hartley': 0, 'Romain Grosjean': 0, 'Sergey Sirotkin': 0}
我想对上面的字典进行子集化,以便只显示前三个键值对。我一直在使用迂回的方式来做到这一点。即,将该字典转换为元组列表,然后将该元组列表子集化为前三个索引并转换回新字典。
output_dict_top3_s3 = list(output_dict_sorted_s2.items())[0:3]
for atuple in output_dict_top3_s3:
print (atuple)
output_dict_s4[atuple[0]] = atuple[1]
有更好的方法吗?
问题 1:您不需要任何外部包,因为您可以使用 lambda:
output_dict_sorted = dict(sorted(output_dict_s1.items(), key=lambda x: x[1],
reverse=True))
问题2:你可以直接将上述指令限制为前3项,因为sorted
returns一个列表:
output_dict_top3 = dict(sorted(output_dict_s1.items(), key=lambda x: x[1], reverse=True)[:3])
如预期的那样给出:
{'Lewis Hamilton': 0.8461538461538461,
'Kimi Raikkonen': 0.8181818181818182,
'Sebastian Vettel': 0.6153846153846154}
3 个问题:
- 有没有一种直接的方法可以让我在不导入外部包的情况下按 a)key 或 b)value 对字典进行降序排序?
- 如何简单地 select 仅按值在字典中排名前 3 的键值对?
- 字典按值排序,然后按键降序排列?
问题 1: 我在“output_dict_s1”下面有以下字典:
{'Sebastian Vettel': 0.6153846153846154, 'Lewis Hamilton': 0.8461538461538461, 'Kimi Raikkonen': 0.8181818181818182, 'Daniel Ricciardo': 0.25, 'Fernando Alonso': 0, 'Max Verstappen': 0.5, 'Nico Hulkenberg': 0, 'Valtteri Bottas': 0.5, 'Stoffel Vandoorne': 0, 'Carlos Sainz': 0, 'Pierre Gasly': 0, 'Kevin Magnussen': 0, 'Marcus Ericsson': 0, 'Esteban Ocon': 0, 'Sergio Perez': 0.14285714285714285, 'Charles Leclerc': 0, 'Lance Stroll': 0, 'Brendon Hartley': 0, 'Romain Grosjean': 0, 'Sergey Sirotkin': 0}
为了按值对字典进行排序,我使用了一个外部包。它可以工作,但我想知道如何在没有包的情况下完成它。
output_dict_sorted = dict(sorted(output_dict_s1.items(), key = operator.itemgetter(1), reverse = True))
问题 2: 现在排序后的字典“output_dict_sorted”看起来像这样:
{'Lewis Hamilton': 0.8461538461538461, 'Kimi Raikkonen': 0.8181818181818182, 'Sebastian Vettel': 0.6153846153846154, 'Max Verstappen': 0.5, 'Valtteri Bottas': 0.5, 'Daniel Ricciardo': 0.25, 'Sergio Perez': 0.14285714285714285, 'Fernando Alonso': 0, 'Nico Hulkenberg': 0, 'Stoffel Vandoorne': 0, 'Carlos Sainz': 0, 'Pierre Gasly': 0, 'Kevin Magnussen': 0, 'Marcus Ericsson': 0, 'Esteban Ocon': 0, 'Charles Leclerc': 0, 'Lance Stroll': 0, 'Brendon Hartley': 0, 'Romain Grosjean': 0, 'Sergey Sirotkin': 0}
我想对上面的字典进行子集化,以便只显示前三个键值对。我一直在使用迂回的方式来做到这一点。即,将该字典转换为元组列表,然后将该元组列表子集化为前三个索引并转换回新字典。
output_dict_top3_s3 = list(output_dict_sorted_s2.items())[0:3]
for atuple in output_dict_top3_s3:
print (atuple)
output_dict_s4[atuple[0]] = atuple[1]
有更好的方法吗?
问题 1:您不需要任何外部包,因为您可以使用 lambda:
output_dict_sorted = dict(sorted(output_dict_s1.items(), key=lambda x: x[1],
reverse=True))
问题2:你可以直接将上述指令限制为前3项,因为sorted
returns一个列表:
output_dict_top3 = dict(sorted(output_dict_s1.items(), key=lambda x: x[1], reverse=True)[:3])
如预期的那样给出:
{'Lewis Hamilton': 0.8461538461538461,
'Kimi Raikkonen': 0.8181818181818182,
'Sebastian Vettel': 0.6153846153846154}