我想从这个复杂的数据最大的得分手,但它显示的错误,也许我错在这里做一些事情。
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
但对于我而言这也是一个关键
你可能想这一点 -
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')
得到的分数为特定的条目。
喔我明白了!现在,我访问来自主词典我的钥匙,然后里面的元素
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'])))