在我的作业中,我有一个文本,我想计算单词在文本中出现的次数。例如,假设我有一个文本文件说。
我有很多猫和狗。我有3只猫和16只狗。我喜欢狗!
由于dogs一词出现了3次,因此我需要输出为该数字。但是,我该如何处理随机文本?
到目前为止,我已经提出了以下建议。
file = open('phrases.txt')
text = file.read()
file.close()
count = countWords()
duplicates = 0
for words in text:
if words #appear twice or more
#if duplicates
duplicates+=1
unique = count - duplicates
#subtract the total, by the amount of duplicates.
print(unique)
countWords()是我制作的另一个函数,该函数计算文本内的总单词数**
答案 0 :(得分:1)
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
答案 1 :(得分:0)
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)