【发布时间】:2020-06-07 17:06:10
【问题描述】:
方向:字典的键应该是单词,值应该是单词在段落中出现的次数,按降序对字典进行排序。仅显示包含 4 个或更多字母的单词。
text = """The goal is to turn data into information and information into insight .
You can have data without information but you cannot have information without data ."""
我一直在解决这个问题,但似乎不能只显示包含 4 个或更多字母的单词。任何帮助将不胜感激,谢谢。
输出应该是
这是我目前所做的
# clean up string 'text'
for char in '-.,\n':
text=text.replace(char,' ')
text = text.lower()
words_list = text.split(' ')
#define dictionary
words_dict = {}
#Count number of times each word comes up in list of words (in dictionary)
for word in words_list:
if word not in words_dict:
words_dict[word] = 0
words_dict[word] += 1
#sort dictionary by number of occurences
sorted(words_dict.items(), key = lambda x: x[1], reverse = True)
【问题讨论】:
标签: python dictionary