Skip to content

Commit 72219b8

Browse files
committed
python ex
1 parent f9c1680 commit 72219b8

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

stepic/python_fullstack/groupby_example.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,20 @@
4848

4949
print([(i, list(j)) for i, j in itertools.groupby(s)])
5050

51-
######################
51+
######################
52+
"""
53+
Напишите функцию, которая принимает на вход строку слов и возвращает словарь,
54+
содержащий частоту каждого слова в строке. Словарь должен включать слова в качестве ключей и их частоты в качестве значений.
55+
"""
56+
57+
58+
from typing import Dict
59+
from itertools import groupby
60+
61+
62+
def word_frequencies(string):
63+
return {i:len(list(j)) for i,j in groupby(sorted(string.split()))}
64+
# Поместите свой код сюда
65+
66+
print(word_frequencies('hello world and hello universe'))
67+
print(word_frequencies('hello world'))

0 commit comments

Comments
 (0)