“Найти частоту чисел в списке Python” Ответ

Как получить частоту каждого элемента в списке Python

# easiest way to count the frequency of all elements in a list
lst = ['Sam', 'Sam', 'Tim', 'Tim', 'Tim', 'r', 'l']

freq = {} # stores the frequency of elements
counting = [freq.update({x: lst.count(x)}) for x in lst]

# output of freq 
{'Sam': 2, 'Tim': 3, 'r': 1, 'l': 1}

#credit: buggyprogrammer.com
#Note: if you print "counting" it will return a list full of None so ignore it. 
Frantic Ferret

Список количества частоты частоты

import collections
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)
print(counter)
# Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})
print(counter.values())
# [4, 4, 2, 1, 2]
print(counter.keys())
# [1, 2, 3, 4, 5]
print(counter.most_common(3))
# [(1, 4), (2, 4), (3, 2)]
Cheerful Cormorant

Найти частоту чисел в списке Python

from collections import Counter

def frequency_table(n):
    table = Counter(n)
    print('Number\tFrequency')
    for number in table.most_common() :
        print('{0}\t{1}'.format(number[0], number[1]))
        
# src : Doing Math With Python
icy_milktea27

Ответы похожие на “Найти частоту чисел в списке Python”

Вопросы похожие на “Найти частоту чисел в списке Python”

Больше похожих ответов на “Найти частоту чисел в списке Python” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования