【发布时间】:2016-12-07 07:19:13
【问题描述】:
我正试图弄清楚如何制作一个程序,该程序接受用户选择的文件(通过输入文件名)并计算用户输入的每个单词的频率。
我有大部分,但是当我输入多个单词供程序查找时,只有第一个单词显示正确的频率,其余显示为“0 次出现”
file_name = input("What file would you like to open? ")
f = open(file_name, "r")
the_full_text = f.read()
words = the_full_text.split()
search_word = input("What words do you want to find? ").split(",")
len_list = len(search_word)
word_number = 0
print()
print ('... analyzing ... hold on ...')
print()
print ('Frequency of word usage within', file_name+":")
for i in range(len_list):
frequency = 0
for word in words:
word = word.strip(",.")
if search_word[word_number].lower() == word.lower():
frequency += 1
print (" ",format(search_word[word_number].strip(),'<20s'),"/", frequency, "occurrences")
word_number = word_number + 1
就像一个示例输出:
What file would you like to open? assignment_8.txt
What words do you want to find? wey, rights, dem
... analyzing ... hold on ...
Frequency of word usage within assignment_8.txt:
wey / 96 occurrences
rights / 0 occurrences
dem / 0 occurrences
我的程序有什么问题?请帮忙:o
【问题讨论】:
-
如果你在
","拆分,你的输入不应该是"wey,rights,dem",没有空格吗?
标签: python file text counter frequency