第一个结构:
dic = {'l':2, 'o':3, 'p':6}
for key in dic:
total = [dic[key]]
print max(total)
输出为6
第二种结构:
dic = {}
print 'Enter line of text'
line = input('')
words = line.split()
print 'Words', words
print 'Counting'
for word in words:
dic[word] = dic.get(word,0) + 1
print 'Dictionary of your text', dic
for num in dic:
total = [dic[num]]
print max(total)
输出为1,大部分为1
答案 0 :(得分:2)
您只需将 last 值放在total
列表中:
for key in dic:
total = [dic[key]]
每次将total
重新绑定到新列表。这取决于字典的顺序,即dependent on the dictionary insertion and deletion history。
如果您想知道最大值,请使用max(dic.values())
。
如果您必须使用循环,那么至少将该值附加到现有列表中:
total = []
for key in dic:
total.append(dic[key])
print(max(total))