Нужно из текста получить частотный словарь, програма выводит ключ и значения, но в каждом новом рядке добавляет по новому ключу и значению
import collections
text = "These people had a little window at the back of their house from which a splendid garden could be seen, which was full of the most beautiful flowers and herbs. It was, however, surrounded by a high wall, and no one dared to go into it because it belonged to an enchantress, who had great power and was dreaded
by all the world."
t = text.replace(',', '')
#print(t)
print(max(t.split(), key=len))
print(min(t.split(), key=len))
print(collections.Counter(text.split()).most_common(1))
ma_dict = {}
for word in text.split():
ma_dict[word] = ma_dict.get(word, 0) + 1
print(ma_dict)
Ответы (1 шт):
Автор решения: KoVadim
→ Ссылка
Ну так логично, Вы печатаете содержимое мапы на каждой итеррации. Удалите пробелы перед print в последней строке все будет заметно лучше.
import collections
text = "These people had a little window at the back of their house from which a splendid garden could be seen, which was full of the most beautiful flowers and herbs. It was, however, surrounded by a high wall, and no one dared to go into it because it belonged to an enchantress, who had great power and was dreaded
by all the world."
t = text.replace(',', '')
#print(t)
print(max(t.split(), key=len))
print(min(t.split(), key=len))
print(collections.Counter(text.split()).most_common(1))
ma_dict = {}
for word in text.split():
ma_dict[word] = ma_dict.get(word, 0) + 1
print(ma_dict)