Python:使用Value作为字典获取前n个键

San*_*ndy 3 python dictionary

我有一个字典,如:

data = {'sachin': {'score': 15000, 'out': 100},
        'Dhoni': {'score': 8000, out: 80},
        'Shewag': {'score': 12000, 'out': 150}}
Run Code Online (Sandbox Code Playgroud)

我希望得到两名得分最高的球员.

所以我尝试过: key = (key for key,value in dd.items() if value['score'] > 'value').next()

缠绕在这里没有成功.

尝试使用链接:在字典中具有最高值的前n个键,以元组作为键

因为Python的新手无法绕过完美的解决方案.

有人可以就此分享一些想法!

输出如:

{'sachin':{'score':15000,'out':100},'Shewag':{'score':12000,'out':150}}
Run Code Online (Sandbox Code Playgroud)

注意:应该是排名前三的玩家,例如我需要前两名,但可以在后期更改.

Mik*_*ler 6

快速回答

排序工作:

>>> dict(sorted(data.items(), key=lambda x: x[1]['score'], reverse=True)[:2])
{'Shewag': {'out': 150, 'score': 12000},
 'sachin': {'out': 100, 'score': 15000}}
Run Code Online (Sandbox Code Playgroud)

在步骤中

您对项目进行排序:

>>> sorted(data.items())
[('Dhoni', {'out': 80, 'score': 8000}),
 ('Shewag', {'out': 150, 'score': 12000}),
 ('sachin', {'out': 100, 'score': 15000})]
Run Code Online (Sandbox Code Playgroud)

这按名称按字母顺序排序.

使用key与定义函数lambda排序由score:

sorted(data.items(), key=lambda x: x[1]['score'])
[('Dhoni', {'out': 80, 'score': 8000}),
 ('Shewag', {'out': 150, 'score': 12000}),
 ('sachin', {'out': 100, 'score': 15000})]
Run Code Online (Sandbox Code Playgroud)

使用reverse先获取最大的之一:

sorted(data.items(), key=lambda x: x[1]['score'], reverse=True)
[('sachin', {'out': 100, 'score': 15000}),
 ('Shewag', {'out': 150, 'score': 12000}),
 ('Dhoni', {'out': 80, 'score': 8000})]
Run Code Online (Sandbox Code Playgroud)

最后,只使用切片的两个第一项,并将元组列表转换为字典dict:

>>> dict(sorted(data.items(), key=lambda x: x[1]['score'], reverse=True)[:2])
{'Shewag': {'out': 150, 'score': 12000},
 'sachin': {'out': 100, 'score': 15000}}
Run Code Online (Sandbox Code Playgroud)

由于字典没有订单,你只知道你有两个得分最高的玩家.没有第一个或第二个概念.如果需要,您可以保留元组列表或转换为OrderedDict保存顺序:

>>> from collections import OrderedDict
>>> OrderedDict(sorted(data.items(), key=lambda x: x[1]['score'], reverse=True)[:2])
OrderedDict([('sachin', {'out': 100, 'score': 15000}),
             ('Shewag', {'out': 150, 'score': 12000})])
Run Code Online (Sandbox Code Playgroud)

做得恰当

为了使其更具可重用性,您可以编写一个函数:

from collections import OrderedDict

def get_top_players(data, n=2, order=False):
    """Get top n players by score. 

    Returns a dictionary or an `OrderedDict` if `order` is true.
    """ 
    top = sorted(data.items(), key=lambda x: x[1]['score'], reverse=True)[:n]
    if order:
        return OrderedDict(top)
    return dict(top)

?
Run Code Online (Sandbox Code Playgroud)

现在您可以将它与您的数据一起使用:

>>> get_top_players(data)
{'Shewag': {'out': 150, 'score': 12000},
 'sachin': {'out': 100, 'score': 15000}}
Run Code Online (Sandbox Code Playgroud)

或设置不同数量的顶级球员:

>>> get_top_players(data, n=3)
{'Dhoni': {'out': 80, 'score': 8000},
 'Shewag': {'out': 150, 'score': 12000},
 'sachin': {'out': 100, 'score': 15000}}
Run Code Online (Sandbox Code Playgroud)

或者让它们按顺序排列:

>>> get_top_players(data, order=True)
OrderedDict([('sachin', {'out': 100, 'score': 15000}),
             ('Shewag', {'out': 150, 'score': 12000})])
Run Code Online (Sandbox Code Playgroud)