if __name__ == "__main__":
# 将句子里面的单词抽取成元组
txt = 'I like python and i use python'
words = txt.split(' ')
print(words)
words_set = set(words)
counts = dict()
for word in words_set:
counts[word] = words.count(word)
print(counts)
from collections import Counter
if __name__ == "__main__":
# 将句子里面的单词抽取成元组
txt = 'I like python and i use python'
words = txt.split(' ')
print(words)
# 一行代码搞定
counter = Counter(words)
print(dict(counter))
本文代码已经归档到github,您可以访问下面的链接获得。 欢迎star该代码仓库
代码地址