【发布时间】:2023-01-22 05:12:54
【问题描述】:
如何打印出每个唯一单词在输入字符串中出现的次数?我已经有了要运行的程序,以便打印出字符串中出现的唯一单词的数量。虽然,我想将它添加到其中,以便它打印出每个唯一单词在输入字符串中出现的次数。
喜欢:
“这里”这个词出现了6次
“网”字出现了7次
“你好”这个词出现了 5 次
等等
from collections import Counter user_text = input("Please enter some text --> ") def word_count(user_text): return(len(user_text.strip().split(" "))) number_of_characters = len(user_text) words = user_text.strip().split(' ') print("You typed", len(user_text), "characters") print("You typed", len(words), "words") print("There are", len(set(words)), "unique words") print("The word", words, "")
【问题讨论】:
标签: python