【问题标题】:Taking words that I have in a list and searching for them within a Text File and getting a count for each word获取列表中的单词并在文本文件中搜索它们并计算每个单词的数量
【发布时间】:2020-10-12 16:59:52
【问题描述】:

我正在学习 Python,目前我有一个文本文件。在该文本文件中,我想在列表 ['mice', parrot'] 中搜索我已经拥有的单词,并计算老鼠和鹦鹉被提及的次数。我目前有。

enter code herewith open('animalpoem.txt', 'r') as animalpoemfile:
data = animalpoemfile.read()
search animals = ['mice', parrot']                               

【问题讨论】:

标签: python


【解决方案1】:

您可以使用collections 模块中的Counter() 方法:

from collections import Counter

with open('t.txt', 'r') as animalpoemfile:
    dct = Counter(animalpoemfile.read().split())

search_animals = ['mice', 'parrot']
    
for animal in search_animals:
    print(animal, dct[animal])

【讨论】:

    【解决方案2】:

    要计算文本文件中特定单词的出现次数,将文本文件的内容读取为字符串,然后使用 String.count() 函数将单词作为参数传递给 count() 函数.

    语法:

    n = String.count(word)
    

    其中word为字符串,count()返回该字符串中word出现的次数。

    所以你可以读取文件并使用 count() 方法。

    #get file object reference to the file
    with open("file.txt", "r") as file:
        #read content of file to string
        data = file.read()
    
    words = ['apple', 'orange']
    
    for word in words:
        print('{} occurred {} times.'.format(word, data.count(word)))
    

    希望这应该可以正常工作。

    注意: 您甚至可以遍历每个单词并增加计数器。但是使用像 Python 这样的高级编程语言,利用这些内置方法将是有益的。

    【讨论】:

      【解决方案3】:

      步骤

      1. 打开文件
      2. 读取文件并存储在变量中
      3. 统计你想在这个变量中搜索的单词
        def num_of_word_in_file(file_name, word):
          with open(file_name) as file:
            content = file.read()
          return content.count(word)
        for animal in animals :
            print(num_of_word_in_file(file_name, animal))
        

      【讨论】:

        【解决方案4】:

        一个示例解决方案:

        import re
        
        animals = ['mice', 'parrot']
        
        # make a dictionary with keys being the names of the animals,
        # and values being initial counts (0)
        counts = dict([(a, 0) for a in animals])
        
        with open("animalpoem.txt") as f:
             for line in f:  # loop over lines of the file, assign to variable "line"
                  for word in re.findall("\w+", line.lower()):  # convert to lower case and make list of words and then iterate over it
                       if word in counts:  # tests if the word is one of the keys in the dictionary
                            counts[word] += 1  # if so, increments the count value associated with that word
        
        print(counts)
        

        animalpoem.txt

        Three blind mice. Three blind mice.
        See how they run. See how they run.
        They all ran after the farmer's wife,
        Who cut off their tails with a carving knife,
        Did you ever see such a sight in your life,
        As three blind mice?
        

        程序输出:

        {'mice': 3, 'parrot': 0}
        

        【讨论】:

          最近更新 更多