我写了下面的代码:Python的 - 在字典中的重复值
def word_len_dict(text):
the_dict = {}
user_input = str(text)
words_list = user_input.split()
for word in words_list:
if len(word) in the_dict:
the_dict[len(word)] += [word]
else:
the_dict[len(word)] = [word]
return the_dict
的文字是:
"the faith that he had had had had an affect on his life"
我的输出是走出来的:
2 - ['an', 'he', 'on']
3 - ['had', 'had', 'had', 'had', 'his', 'the']
4 - ['life', 'that']
5 - ['faith']
6 - ['affect']
我会像我的输出是:
2 - ['an', 'he', 'on']
3 - ['had', 'his', 'the']
4 - ['life', 'that']
5 - ['faith']
6 - ['affect']
当我将它添加到字典时,我该如何阻止它重复值?
端起来只是让没有 – Nume