单词频率计数器Python



与此练习作斗争,此练习必须使用字典并计算每个单词在多个用户输入中出现的次数。它以某种方式工作,但不会将用户输入的每一行中的每个单词原子化。因此,它给了我1 x快乐的日子,而不是将"快乐的日子"的输入计算为1 x快乐和1 x天。我尝试过split()和lower(),但这会将输入转换为列表,然后我很难将该列表放入字典。

正如你可能已经猜到的,我是一个新手,所以所有的帮助都将不胜感激!

occurrences = {}
while True:
    word = input('Enter line: ')
    word = word.lower() #this is also where I have tried a split()
    if word =='':
        break
occurrences[word]=occurrences.get(word,0)+1
for word in (occurrences):
    print(word, occurrences[word])

编辑

为回应干杯。这最终成为了最终的解决方案。他们并不担心这个案例,希望最终结果排序()。

occurrences = {}
while True:
    words = input('Enter line: ')
    if words =='':
        break
    for word in words.split(): 
        occurrences[word]=occurrences.get(word,0)+1
for word in sorted(occurrences):
    print(word, occurrences[word])

您所拥有的几乎已经存在,您只想在将单词添加到dict 时循环使用它们

occurrences = {}
while True:
    words = input('Enter line: ')
    words = words.lower() #this is also where I have tried a split()
    if words =='':
        break
    for word in words.split(): 
        occurrences[word]=occurrences.get(word,0)+1
    for word in (occurrences):
        print(word, occurrences[word])

此行不执行:occurrences[word]=occurrence.get(word,0)+1

因为如果它进入if,它就会进入break,并且永远不会执行该行。若不缩进,使其处于外部。

一般来说,发布的代码的缩进是混乱的,我想在实际的代码中并不是这样。

您想要逐行统计数据还是想要整体统计数据?我猜你想要一行接一行,但你也可以通过取消以下代码中的几行来轻松地获得总体统计数据:

# occurrences = dict()  # create a dictionary here if yuo want to have incremental overall stats
while True:
    words = input('Enter line: ')
    if words =='':
        break
    word_list = words.lower().split()
    print word_list
    occurrences = dict()  # create a dict here if you want line by line stats
    for word in word_list:
        occurrences[word] = occurrences.get(word,0)+1
    ## use the lines bellow if you want line by line stats
    for k,v in occurrences.items():
        print k, " X ", v
## use the lines bellow if you want overall stats
# for k,v in occurrences.items():
    # print k, " X ", v

最新更新