Python实现wordcount
def wordcount(text):
words = text.split()
ans={}
for word in words:
if word not in ans:
ans[word] = 1
else:
ans[word] += 1
return ans
text = """Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
res = wordcount(text)
print(res)