In [1]:
import collections

In [2]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
c = collections.Counter(l)

In [3]:
print(c)


Counter({'a': 4, 'c': 2, 'b': 1})

In [4]:
print(type(c))


<class 'collections.Counter'>

In [5]:
print(issubclass(type(c), dict))


True

In [6]:
print(c['a'])


4

In [7]:
print(c['b'])


1

In [8]:
print(c['c'])


2

In [9]:
print(c['d'])


0

In [10]:
print(c.keys())


dict_keys(['a', 'b', 'c'])

In [11]:
print(c.values())


dict_values([4, 1, 2])

In [12]:
print(c.items())


dict_items([('a', 4), ('b', 1), ('c', 2)])

In [13]:
print(c.most_common())


[('a', 4), ('c', 2), ('b', 1)]

In [14]:
print(c.most_common()[0])


('a', 4)

In [15]:
print(c.most_common()[-1])


('b', 1)

In [16]:
print(c.most_common()[0][0])


a

In [17]:
print(c.most_common()[0][1])


4

In [18]:
print(c.most_common()[::-1])


[('b', 1), ('c', 2), ('a', 4)]

In [19]:
print(c.most_common(2))


[('a', 4), ('c', 2)]

In [20]:
values, counts = zip(*c.most_common())

In [21]:
print(values)


('a', 'c', 'b')

In [22]:
print(counts)


(4, 2, 1)

In [23]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
c = collections.Counter(l)

In [24]:
print(len(c))


3

In [25]:
print(set(l))


{'b', 'a', 'c'}

In [26]:
print(len(set(l)))


3

In [27]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

In [28]:
print([i for i in l if l.count(i) >= 2])


['a', 'a', 'a', 'a', 'c', 'c']

In [29]:
print(len([i for i in l if l.count(i) >= 2]))


6

In [30]:
c = collections.Counter(l)

In [31]:
print([i[0] for i in c.items() if i[1] >= 2])


['a', 'c']

In [32]:
print(len([i[0] for i in c.items() if i[1] >= 2]))


2

In [33]:
s = 'government of the people, by the people, for the people.'

In [34]:
s_remove = s.replace(',', '').replace('.', '')

print(s_remove)


government of the people by the people for the people

In [35]:
word_list = s_remove.split()

print(word_list)


['government', 'of', 'the', 'people', 'by', 'the', 'people', 'for', 'the', 'people']

In [36]:
print(word_list.count('people'))


3

In [37]:
print(len(set(word_list)))


6

In [38]:
c = collections.Counter(word_list)

In [39]:
print(c)


Counter({'the': 3, 'people': 3, 'government': 1, 'of': 1, 'by': 1, 'for': 1})

In [40]:
print(c.most_common()[0][0])


the

In [41]:
s = 'supercalifragilisticexpialidocious'

In [42]:
print(s.count('p'))


2

In [43]:
c = collections.Counter(s)

In [44]:
print(c)


Counter({'i': 7, 's': 3, 'c': 3, 'a': 3, 'l': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'o': 2, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1})

In [45]:
print(c.most_common(5))


[('i', 7), ('s', 3), ('c', 3), ('a', 3), ('l', 3)]

In [46]:
values, counts = zip(*c.most_common(5))

print(values)


('i', 's', 'c', 'a', 'l')