如何使用for循环在字典中查找键的最大值

时间:2019-05-19 13:34:10

标签: python

当我到达需要寻找最高值和与之相对应的键的阶段时,我有一个程序制作的字典。我当前的代码给我错误的值和不匹配的密钥。

我当前的代码如下所示。我不能在此问题中使用诸如max()之类的任何功能

mail = None
bigcount = None
for id,count in table.items():
    if count > bigcount or bigcount is None:
        mail = id
        bigcount = count

print(mail, bigcount)

预期:cwen@iupui.edu 5

实际:david.horwitz@uct.ac.za 1

此外,david.horwitz @ uct.ac.za的字典条目为4,而不是此处显示的1

0 个答案:

没有答案