-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_most_common.py
54 lines (39 loc) · 1.28 KB
/
counter_most_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Counter - це most_common(), який повертає список елементів та їх частоту
import collections
student_marks = [4, 2, 4, 6, 7, 4, 2 , 3, 4, 5, 6, 6, 7 , 1, 1, 1, 3, 5]
mark_counts = collections.Counter(student_marks)
print(mark_counts.most_common())
print(mark_counts.most_common(1))
print(mark_counts.most_common(2))
'''[(4, 4), (6, 3), (1, 3), (2, 2), (7, 2), (3, 2), (5, 2)]
[(4, 4)]
[(4, 4), (6, 3)]
'''
#============================================
'''
Наприклад, якщо вам потрібно підрахувати кількість кожної літери у рядку,
'''
from collections import Counter
# Створення Counter з рядка
letter_count = Counter("banana")
print(letter_count)
'''
Counter({'a': 3, 'n': 2, 'b': 1})
'''
#============================================
'''Виконати підрахунок слів в тексті теж стає досить простою задачею:
'''
sentence = "the quick brown fox jumps over the lazy dog"
words = sentence.split()
word_count = Counter(words)
# Виведення слова та його частоти
for word, count in word_count.items():
print(f"{word}: {count}")
'''the: 2
quick: 1
brown: 1
fox: 1
jumps: 1
over: 1
lazy: 1
dog: 1'''