计算文件中单词的出现次数

时间:2020-09-22 17:00:10

标签: python

我是Python的新手,我也不知道该怎么做。所以这是一个问题:

编写一个函数,该函数需要一个文件名和一个单词(或者,如果没有给出单词,则假定单词为“ hello”),并返回一个整数,表示该单词在文件中出现的次数,文件的第一行不计算在内。

2 个答案:

答案 0 :(得分:2)

用两个参数调用该函数,一个必须在路径中的文件名和要在文件中计数的单词。如果您不输入单词,该函数将使用默认单词“ hello”。

def wordcount(filename, word="hello"):
    # Open the file name
    with open(filename) as file:
        # Skip first line
        next(file)  
        # read the file as a string except the first line and count the occurrences 
        return file.read().count(word)

Count方法返回给定字符串中子字符串出现的次数。您也可以保存第一行x = next(file),以备日后使用。

调用函数并用print(wordcount("sample.txt", "repeat"))打印结果以计算单词“ repeat”在文件中出现的次数。

sample.txt包含:

Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !

结果必须为4:)

答案 1 :(得分:1)

from pathlib import Path

def count(filename: str, word = "hello"):
    file = Path(filename)
        
    text = file.read_text()
    lines_excluding_first = text.split("\n")[1:]
    
    counts = sum([sum(list(map(lambda y: 1 if word == y else 0, x.split(" ")))) for x in lines_excluding_first])
    
    
    return counts

示例: 说您有一个txt文件,例如:

sample.txt
----------

this is the first line so this dose not count!
hello!! this is a sample text.
text which will be used as sample.
if nothing no word specified hellow is returned!
it will return the count.
print(count("sample.txt")) 
## output: 2

编辑:

我现在在代码中做了一些小的更正,"hellow!!""hellow"是两个单独的单词。单词之间用空格隔开,然后检查是否相等。因此"hellow"和“ hellow”。也不同!

根据这里的请求,它在repl.it上的外观:

首先创建 sample.txt sample.txt main.py 如下所示: main.py 您可以将默认“ hello”的输出显示为2
[output is case sensitive i.e Hello and hello are not the same]