在没有计数功能的情况下计算文本文件中单词的出现次数
Counting occurrences of a word in a text file without count function
我正在学习 Python,我应该创建一个程序来获取用户输入的单个单词,然后 returns 该单词在文本文件中出现的次数.我真的不知道我做错了什么或如何解决这个问题,一直在看视频等几个小时。我无法使用 `.count() 函数。
这是我的代码:
import string
frank = open('Frankenstein.txt','r',encoding='utf-8')
frank_poem = frank.read()
frank_poem = frank_poem.translate(str.maketrans('','',string.punctuation))
split_poem = frank_poem.split()
word = input("Please enter the word you would like to count in Frankenstein (case-sensitive): ")
def word_count(word):
total = 0
for word in split_poem:
total += word
return total
print(word_count(word))
total += word
正在将字符串 word
添加到 total
。您可能正在寻找 total += 1
,而不是 total += word
。然后,您需要添加一个 if
语句来检查您当前正在检查的词是否等同于目标词,如下所示:
def word_count(target_word):
total = 0
for word in split_poem:
if word == target_word:
total += 1
return total
这是一个可行的解决方案:
def word_count(word):
total = 0
for poem_word in split_poem:
if word == poem_word:
total += 1
return total
print(word_count(word))
这里有一个更简洁但不使用count
的替代方案。它依赖于 True
的 boolean
值(例如当 poem_word == word
为 True
时)被转换为 1
的整数值 built-in 函数 sum()
.
def word_count(word):
return sum(poem_word == word for poem_word in split_poem)
import string
frank = open('Frankenstein.txt','r',encoding='utf-8')
frank_poem = frank.read()
frank_poem = frank_poem.translate(str.maketrans('','',string.punctuation))
split_poem = frank_poem.split()
word = input("Please enter the word you would like to count: ")
def word_count():
total = 0
for wordi in split_poem:
if wordi == word:
total += 1
return total
print(word_count())
我正在学习 Python,我应该创建一个程序来获取用户输入的单个单词,然后 returns 该单词在文本文件中出现的次数.我真的不知道我做错了什么或如何解决这个问题,一直在看视频等几个小时。我无法使用 `.count() 函数。
这是我的代码:
import string
frank = open('Frankenstein.txt','r',encoding='utf-8')
frank_poem = frank.read()
frank_poem = frank_poem.translate(str.maketrans('','',string.punctuation))
split_poem = frank_poem.split()
word = input("Please enter the word you would like to count in Frankenstein (case-sensitive): ")
def word_count(word):
total = 0
for word in split_poem:
total += word
return total
print(word_count(word))
total += word
正在将字符串 word
添加到 total
。您可能正在寻找 total += 1
,而不是 total += word
。然后,您需要添加一个 if
语句来检查您当前正在检查的词是否等同于目标词,如下所示:
def word_count(target_word):
total = 0
for word in split_poem:
if word == target_word:
total += 1
return total
这是一个可行的解决方案:
def word_count(word):
total = 0
for poem_word in split_poem:
if word == poem_word:
total += 1
return total
print(word_count(word))
这里有一个更简洁但不使用count
的替代方案。它依赖于 True
的 boolean
值(例如当 poem_word == word
为 True
时)被转换为 1
的整数值 built-in 函数 sum()
.
def word_count(word):
return sum(poem_word == word for poem_word in split_poem)
import string
frank = open('Frankenstein.txt','r',encoding='utf-8')
frank_poem = frank.read()
frank_poem = frank_poem.translate(str.maketrans('','',string.punctuation))
split_poem = frank_poem.split()
word = input("Please enter the word you would like to count: ")
def word_count():
total = 0
for wordi in split_poem:
if wordi == word:
total += 1
return total
print(word_count())