Python 如何计算每个单词在文本文件中出现的次数?

Python 如何计算每个单词在文本文件中出现的次数?,python,Python,我正在尝试编写一个程序,计算一个单词出现的具体次数。我试过做这件事,但运气不好。输出必须为如下所示的代码: a:14 不是:1 注意:1 今天:1 胜利:1 我们:1 write_index_file函数写入名为outfile的索引文件,其中包含单词dict字典的元素。在写入文件时检查异常。单词应该按字母顺序排列。您可以将字典中的“word:count”对保存到列表中,然后使用list.sort()方法创建排序列表。在保存到文件之前将所有单词转换为小写(使用str.lower()方法执行此操作

我正在尝试编写一个程序,计算一个单词出现的具体次数。我试过做这件事,但运气不好。输出必须为如下所示的代码:

a:14
不是:1
注意:1
今天:1
胜利:1
我们:1
write_index_file函数写入名为outfile的索引文件,其中包含单词dict字典的元素。在写入文件时检查异常。单词应该按字母顺序排列。您可以将字典中的“word:count”对保存到列表中,然后使用list.sort()方法创建排序列表。在保存到文件之前将所有单词转换为小写(使用str.lower()方法执行此操作)。 def写入索引文件(word dict,outfile): “”测试函数 #打印索引

        >>> word_dict = {'We': {1}, 'observe': {1}, 'today': {1}, 'not': {1}, 'a': {1, 2, 4}, 'victory': {1}}
        >>> write_index_file(word_dict,"testout.txt")
        Index saved to: testout.txt
        >>> with open("testout.txt") as f:  
    print(f.read())
        a: 1 2 4 
        not: 1 
        observe: 1 
        today: 1 
        victory: 1 
        we: 1 
        <BLANKLINE>
    """
          
    try:
    # Open the file.
    outputfile = open(outfile, 'w')

    key_value_list = []
        # Write the entries from the dictionary to a list of 'word:count' entries e.g. a: 1 2 4. 
        # You will need nested loops -   for key in dict...for value in dict[key].. to build out your
        # list of strings.  Convert words to lowercase using str.lower() before creating your 'word:count' 
        # string.  You can then sort the list using list.sort().
        
    for key in word_dict:
        key_value_list.append(key)
        for value in word_dict[key]:
            key_value_list.sort()
                 
        # Write the entries from the dictionary.
    for element in key_value_list:
        if word_dict[key] != "\n":
            if word_dict[key].lower() in word_dict[key]:
                word_dict[key] = word_dict[key]+1
            else:
                word_dict[key] = 1 
                
outputfile.write(element+'\n')
        
        # Close the file.
    outputfile.close()
        
    print("Index saved to:",outfile)

    except Exception as err:
        print("Error writing the file:",outfile)
        print(err)
        
if __name__ == '__main__':
    # test function
    # print the index
    word_dict = {'we:': {1}, 'observe:': {1}, 'today:': {1}, 'not:': {1}, 'a:': {1, 2, 4}, 'victory:': {1}}
    write_index_file(word_dict,"testout.txt")
    with open("testout.txt") as f:  print(f.read())
>>word_dict={'We':{1},'observe':{1},'today':{1},'not':{1},'a':{1,2,4},'victory':{1}
>>>编写索引文件(word-dict,“testout.txt”)
索引保存到:testout.txt
>>>将open(“testout.txt”)作为f:
打印(f.read())
a:14
不是:1
注意:1
今天:1
胜利:1
我们:1
"""
尝试:
#打开文件。
outputfile=open(输出文件“w”)
键值列表=[]
#将字典中的词条写入“word:count”词条列表中,例如a:1 2 4。
#您将需要嵌套循环-用于dict中的键…用于dict[key]中的值…以构建您的
#字符串列表。在创建“word:count”之前,使用str.lower()将单词转换为小写
#然后可以使用list.sort()对列表进行排序。
对于键入的单词_dict:
键\值\列表。追加(键)
对于word_dict[key]中的值:
key\u value\u list.sort()
#从字典中写出词条。
对于键值列表中的元素:
如果单词dict[key]!=“\n”:
如果word_dict[key].lower()在word_dict[key]中:
单词dict[key]=单词dict[key]+1
其他:
单词dict[key]=1
outputfile.write(元素+'\n')
#关闭文件。
outputfile.close()
打印(“索引保存到:”,输出文件)
除异常作为错误外:
打印(“写入文件时出错:”,输出文件)
打印(错误)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#测试功能
#打印索引
单词dict={'we:':{1},'观察:':{1},'今天:':{1},'不是:':{1},'a:':{1,2,4},'胜利:':{1}
编写索引文件(word-dict,“testout.txt”)
使用open(“testout.txt”)作为f:print(f.read())

任何帮助都将不胜感激。谢谢!

这将计算您的单词,并将它们放入词典中

list_of_words = ["hey", "bye", "code"]
text = "hey, lots of cool code you have there. Bye!"
punctuations = "!()-[]{};:'\"\\,<>./?@#$%^&*_~"
for word in text.split():
    word = word.strip(punctuations)
    if word in dic:
        dic[word] += 1
单词列表=[“嘿”,“再见”,“代码”]
text=“嘿,这里有很多很酷的代码。再见!”
标点符号=“!()-[]{};”:“\”\,./?@$%^&*\
对于text.split()中的单词:
word=word.strip(标点符号)
如果dic中有单词:
dic[字]+=1

应该做这个把戏

尽管另一个问题是询问一些其他细节,但它也包含了这个小问题的答案。有兴趣知道
'a':{1,2,4}
的实际含义。
txt = [["Hello", "Hello", "Carole","Bob","Hello"]
duplicates = {}
for word in txt:
    pass
    duplicates[word] = duplicates.get(word, 0) + 1
for r in duplicates:
    print(r, "occured", duplicates[r], "times")
    pass