0
我一直在试图产生一个程序的输出:Python的初学者问题 - 字典
Enter line: which witch
Enter line: is which
Enter line:
is 1
which 2
witch 1
我它是如何想的工作是让你输入几行,并没有被提交时它会统计每一行的数量。
目前,我无法计算句子中的单个行,但只能计算整个句子。我的代码:
dic = {}
while True:
line = input('Enter Line: ')
line = line.lower()
if not line:
break
dic.setdefault(line, 0)
dic[line] += 1
for line, n in sorted(dic.items()):
print(line, n)
将会产生输出:
Enter line: which witch
Enter line: is which
Enter line:
which witch 1
is which 1
,而不是第一个
任何帮助,将不胜感激。谢谢
...或者使用'dic [word] = dic.get(word,0)+ 1'而不是'setdefault'这两行代码(它自己调用'get()'并返回一个值'不使用)。 –
谢谢。我一直在这上面花钱! – user3879060