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

2024-04-19 07:53:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,不知道该怎么办。这就是问题所在:

编写一个函数,该函数接受一个文件名和一个单词(或者,如果没有给出单词,则假定单词为“hello”),并返回一个整数,表示该单词在文件中出现的次数,但文件第一行中的实例除外


Tags: 文件实例函数hello文件名整数单词次数
2条回答
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.”也不同

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

首先制作一个sample.txtsample.txtmain.py看起来像: main.py您可以看到默认“hello”的输出为2
[output is case sensitive i.e Hello and hello are not the same]

使用两个参数调用函数,一个文件名必须位于path中,另一个单词必须在文件中计数。如果不传递单词,函数将采用默认单词“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:)

相关问题 更多 >