我刚开始编程。我正在制作一个项目,在这个项目中,我可以统计一篇文章或一本小说中出现的单词数量,然后程序会打印这个单词以及它在文章中重复的次数。我在程序中使用字典。if else语句和字典在Python中
之后,我提示用户插入一个单词,程序将尝试查找该单词发生了多少次(如果有的话)。不过,我的最后一条陈述有问题。如果该单词不存在,那么“print”(该单词不存在于插入的文件“)”中会一再重复。我怎样才能解决它,所以它只打印一次?
这里是我的代码:
from string import *
import codecs
def removePunctuation(sentence):
new_sentence = ""
for char in sentence:
if char not in punctuation:
new_sentence = new_sentence + char
return new_sentence
def wordFrequences(new_sentence):
wordFreq = {}
split_sentence = new_sentence.split()
for word in split_sentence:
wordFreq[word] = wordFreq.get(word,0) + 1
wordFreq.items()
return (wordFreq)
#=====================================================
def main():
fileName = open("arabic.txt","r")
#fileName = open("arabic.txt","r",encoding="utf-8")
new_sentence = removePunctuation(fileName)
D = wordFrequences(new_sentence)
#print(D)
excel = open("file.csv", "w")
excel.write("words in article" + "\t" + "frequency" + "\n\n")
for i in D:
#print(i , D[i])
excel.write(i + "\t" + str(D[i]) + "\n")
prompt = input("insert a word for frequency: ")
found = True
for key in D:
if key == prompt:
print(key, D[key])
break
else:
print("the word does not exist in the file inserted")
main()
Python有于类别的东西,已经做了这个,仅供参考。 '这是一个无序的集合,元素被存储为字典键,并且它们的计数被存储为字典值。' http://docs.python.org/2/library/collections.html – cdhagmann