collections.Counter(string).most_common code example
Example 1: collections.Counter(string).most_common
counter = Counter({'Dog': 2, 'Cat': -1, 'Horse': 0})
most_common_element = counter.most_common(1)
print(most_common_element)
least_common_element = counter.most_common()[:-2:-1]
print(least_common_element)
Example 2: collections.Counter(string).most_common
c1 = Counter(a=2, b=0, c=-1)
c2 = Counter(a=1, b=-1, c=2)
c = c1 + c2
print(c)
c = c1 - c2
print(c)
c = c1 & c2
print(c)
c = c1 | c2
print(c)
Example 3: collections.Counter(string).most_common
counter = Counter('ababab')
print(counter)
c = Counter('abc')
print(c)
counter.subtract(c)
print(counter)
counter.update(c)
print(counter)
Example 4: collections.Counter(string).most_common
counter = Counter({'Dog': 2, 'Cat': -1, 'Horse': 0})
elements = counter.elements()
for value in elements:
print(value)
Example 5: collections.Counter(string).most_common
del counter['Unicorn']
print(counter)
Example 6: collections.Counter(string).most_common
counter = Counter({'Dog': 2, 'Cat': 1, 'Horse': 1})
counter['Horse'] = 0
print(counter)
counter['Unicorn'] = 1
print(counter)
Example 7: collections.Counter(string).most_common
print(counter['Unicorn'])
Example 8: collections.Counter(string).most_common
counter = Counter({'Dog': 2, 'Cat': 1, 'Horse': 1})
countDog = counter['Dog']
print(countDog)
Example 9: collections.Counter(string).most_common
special_counter = Counter(name='Pankaj', age=20)
print(special_counter)
Example 10: collections.Counter(string).most_common
counter = Counter('abc')
print(counter)
words_list = ['Cat', 'Dog', 'Horse', 'Dog']
counter = Counter(words_list)
print(counter)
word_count_dict = {'Dog': 2, 'Cat': 1, 'Horse': 1}
counter = Counter(word_count_dict)
print(counter)