Python 3.x 在字典中查找最大值的键

Python 3.x 在字典中查找最大值的键,python-3.x,dictionary,output,Python 3.x,Dictionary,Output,基本上我有这个代码,我需要一个特定的输出,在那里我陈述了获胜者和他的票数。我似乎已经找到了最大值,但它不是关键的对应物。错误在我的倒数第二个输出中。让我知道你们的想法,这可能是一个简单的解决方案,谢谢 print() print() print() import sys fo = open(sys.argv[1], "r") dic = {} count = 0 winner = 0 print("Candidates".center(15), "Votes".rjust(10), "Perc

基本上我有这个代码,我需要一个特定的输出,在那里我陈述了获胜者和他的票数。我似乎已经找到了最大值,但它不是关键的对应物。错误在我的倒数第二个输出中。让我知道你们的想法,这可能是一个简单的解决方案,谢谢

print()
print()
print()
import sys
fo = open(sys.argv[1], "r")
dic = {}
count = 0
winner = 0

print("Candidates".center(15), "Votes".rjust(10), "Percent".rjust(10))
print("==========".center(15), "=====".rjust(10), "=======".rjust(10))

for line in fo:
    line = line[:-1]
    x = line.split(" ")
    names = (x[0]) + " " + (x[1])
    votes = int(x[2]) + int(x[3]) + int(x[4]) + int(x[5])
    dic[names] = votes
    count = votes + count
    if winner < votes:
        winner = votes

for i in dic.keys():
    percent = int((dic[i]/count)*100.00)
    print (i.center(15),str(dic[i]).center(15),str(percent)+"%")

#Loop through every kid and find percentage, 
print()
print("The winner is", "" , "with", winner, "votes!")
print()
print("Total votes polled:", count)
print()
print()
print()
print()
打印()
打印()
导入系统
fo=打开(sys.argv[1],“r”)
dic={}
计数=0
获胜者=0
打印(“候选人”。中间(15),“选票”。rjust(10),“百分比”。rjust(10))
打印(“==========”.center(15),“====”.rjust(10),“=======”.rjust(10))
对于fo中的行:
行=行[:-1]
x=行分割(“”)
名称=(x[0])+“”+(x[1])
投票数=int(x[2])+int(x[3])+int(x[4])+int(x[5]))
dic[姓名]=投票
计数=投票数+计数
如果获胜者未投票:
获胜者=选票
对于dic.keys()中的i:
百分比=整数((dic[i]/计数)*100.00)
打印(i.center(15),str(dic[i])。center(15),str(百分比)+“%”)
#循环遍历每个孩子,找出百分比,
打印()
打印(“获胜者是“,”,“有”,获胜者,“投票!”)
打印()
打印(“投票总数:”,计数)
打印()
打印()
打印()

既然您只寻找赢家,为什么还要将其他人都存储在字典中?只需在两个变量中记录获胜者的姓名和得分,最后只需打印这些。还要打印号码,请执行此操作
str(winner)
ughh。可以这是有道理的。那么,如果winnerimport operator dic = {'a':1000, 'b':3000, 'c': 100} max(dic.items(), key=operator.itemgetter(1))[0]