In [1]:
import collections
In [2]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
c = collections.Counter(l)
In [3]:
print(c)
In [4]:
print(type(c))
In [5]:
print(issubclass(type(c), dict))
In [6]:
print(c['a'])
In [7]:
print(c['b'])
In [8]:
print(c['c'])
In [9]:
print(c['d'])
In [10]:
print(c.keys())
In [11]:
print(c.values())
In [12]:
print(c.items())
In [13]:
print(c.most_common())
In [14]:
print(c.most_common()[0])
In [15]:
print(c.most_common()[-1])
In [16]:
print(c.most_common()[0][0])
In [17]:
print(c.most_common()[0][1])
In [18]:
print(c.most_common()[::-1])
In [19]:
print(c.most_common(2))
In [20]:
values, counts = zip(*c.most_common())
In [21]:
print(values)
In [22]:
print(counts)
In [23]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
c = collections.Counter(l)
In [24]:
print(len(c))
In [25]:
print(set(l))
In [26]:
print(len(set(l)))
In [27]:
l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
In [28]:
print([i for i in l if l.count(i) >= 2])
In [29]:
print(len([i for i in l if l.count(i) >= 2]))
In [30]:
c = collections.Counter(l)
In [31]:
print([i[0] for i in c.items() if i[1] >= 2])
In [32]:
print(len([i[0] for i in c.items() if i[1] >= 2]))
In [33]:
s = 'government of the people, by the people, for the people.'
In [34]:
s_remove = s.replace(',', '').replace('.', '')
print(s_remove)
In [35]:
word_list = s_remove.split()
print(word_list)
In [36]:
print(word_list.count('people'))
In [37]:
print(len(set(word_list)))
In [38]:
c = collections.Counter(word_list)
In [39]:
print(c)
In [40]:
print(c.most_common()[0][0])
In [41]:
s = 'supercalifragilisticexpialidocious'
In [42]:
print(s.count('p'))
In [43]:
c = collections.Counter(s)
In [44]:
print(c)
In [45]:
print(c.most_common(5))
In [46]:
values, counts = zip(*c.most_common(5))
print(values)