如何从嵌套的数据值

问题描述 投票:1回答:2

我想从这个复杂的数据最大的得分手,但它显示的错误,也许我错在这里做一些事情。

students = {
    'harshit':{'score':90, 'age':22},
    'Mohit':{'score':79, 'age':20},
    'Rohit':{'score':99, 'age':29}
}

print((max(students , key= lambda item: item.get('score'))))

我所期望的输出:罗希特 但在这里我得到了一个错误cox they're saying item is a string但对于我而言这也是一个关键

python-3.x
2个回答
2
投票

你可能想这一点 -

students = {
    'harshit':{'score':90, 'age':22},
    'Mohit':{'score':79, 'age':20},
    'Rohit':{'score':99, 'age':29}
}

print((max(students , key= lambda item: students[item].get('score'))))

通过拉姆达您指定的关键。所以students[item]遍历students然后get('score')得到的分数为特定的条目。


0
投票

喔我明白了!现在,我访问来自主词典我的钥匙,然后里面的元素

students = {
    'harshit':{'score':9, 'age':22},
    'Mohit':{'score':9, 'age':20},
    'Rohit':{'score':9, 'age':29}
}
print((max(students , key= lambda item: students[item]['score'])))
© www.soinside.com 2019 - 2024. All rights reserved.