读取文本文件并计算单词重复的次数。使用 .split 函数。现在希望它忽略区分大小写

reading a text file and counting how many times a word is repeated. Using .split function. Now wants it to ignore case sensitive

目前获得所需的输出。

程序提示用户搜索一个词。

用户输入,程序读取文件并给出输出。

'ashwin: 2'

现在我想让它忽略区分大小写。例如,"Ashwin" 和 "ashwin" 都应 return 2,因为它在文本文件中包含两个 ashwin。

def word_count():
    file = "test.txt"
    word = input("Enter word to be searched:")
    k = 0

    with open(file, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                if i == word:
                    k = k + 1
    print(word + ": " + str(k))


word_count()

您可以使用 lower() 来比较这部分的字符串 if i.lower() == word.lower():

例如:

def word_count():
    file = "test.txt"
    word = input("Enter word to be searched:")
    k = 0

    with open(file, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                if i.lower() == word.lower():
                    k = k + 1
    print(word + ": " + str(k))


word_count()

使用集合中的计数器 class,returns 具有键值对的字典可以使用 O(1) 时间访问。

from collections import Counter

def word_count():
    file = "test.txt"

    with open(file, 'r') as f:
         words = f.read().replace('\n', '').lower().split()
         count = Counter(words)

    word = input("Enter word to be searched:")
    print(word, ":", count.get(word.lower()))

您可以在行和单词上使用 .lower 来消除大小写。 或者你可以使用内置的 re 模块。

len(re.findall(word, text, flags=re.IGNORECASE))