Python Tally Count of List Items Using Collections Counter

preview_player
Показать описание
In this Python tutorial, we will go over how to create a tally count of list elements using the collections module Counter function.
Рекомендации по теме
Комментарии
Автор

Below are a few more examples to get different counts:

l = ['a', 'a', 'b', 'a', 'b', 'c', 'a', 'a', 'b', 'c', 'b', 'c']

# total count
total_count = len(l)
print(total_count)
# prints 12

# tally count
d = {}
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
print(d)
# prints {'a': 5, 'b': 4, 'c': 3}

# count of unique values
print(len(set(l)))
# prints 3

RyanNoonan