我在这里的工作是计算“文本”中唯一的单词。它基于上一部分代码。
A)
def read_book(text):
try:
file = open(text, "r")
content = file.read()
file.close()
except:
return None
return content
B)
from collections import defaultdict
def word_counts(book):
book = book.lower()
word_list = book.split()
count = defaultdict(int)
for word in word_list:
if len(word) > 2:
count[word] += 1
return count
C)
def total_words(contents):
total_sum = 0
counts = word_counts(contents)
total_sum = sum(counts.values())
return total_sum
D)
def unique_words(string):
unique = []
words = string.split()
for words in unique:
w = word.lower()
word_counts[w]+=1
print("unique words:{}".format(len(word_counts.keys)))
return
我收到D的断言错误。