Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Counter is a subclass of dict() that counts the number of elements in a hashtable, and returns a dictionary with keys as elements and values as the number of elements

from collections import Counter str1 = "abcbcaccbbad" li = ["a", "b", "c", "a", "b", "b"] dict1 = {"1": 3, "3": 2, "17" : 2} #Counter Print ("Counter(s):", Counter(str1)) print ("Counter(li):", Counter(li)) print ("Counter(d):", Counter(d)Copy the code

result:

Counter(s): Counter({'b': 4, 'c': 4, 'a': 3, 'd': 1})
Counter(li): Counter({'b': 3, 'a': 2, 'c': 1})
Counter(d): Counter({'1': 3, '3': 2, '17': 2})
Copy the code

Several of these important methods are described below

1 most_common method

Returns a dictionary of elements sorted by the number of occurrences

1.1 Default Scenarios

By default, the dictionary of all elements is returned

print(Counter(str1).most_common())
Copy the code

result:

[('b', 4), ('c', 4), ('a', 3), ('d', 1)]
Copy the code

1.2 Specified quantity

If n is specified, returns the dictionary of the first n bits of the sorted element

print(Counter(str1).most_common(2))
Copy the code

result:

[('b', 4), ('c', 4)]
Copy the code

2 elements method

Returns the iterator of the element after calculator Counter

2.1 Dictionary situation

This returns an element iterator with value key

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print("sorted(str_iter):", sorted(dict_counter.elements()))
Copy the code

result:

sorted(str_iter): ['1', '1', '1', '17', '17', '3', '3']
Copy the code
print(list(dict_counter.elements()))
Copy the code

result:

['1', '1', '1', '3', '3', '17', '17']
Copy the code

As you can see, the value of ‘1’ is 3, so three ‘1s’ are returned. And so on

2.2 other

This returns an iterator for all elements

str1 = "abcbcaccbbad"
str1 = Counter(str1)
print ("sorted(str1.elements()):", sorted(str1.elements()))
Copy the code

result:

sorted(str1.elements()): ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd']
Copy the code
print(list(str1.elements()))
Copy the code

result:

['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd']
Copy the code

3 the update method

Update the elements inside Counter

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2})
Copy the code

3.1 Dictionary Situation

dict_counter.update({'key':123})
print(dict_counter)
Copy the code

result:

Counter({'key': 123, '1': 3, '3': 2, '17': 2})
Copy the code
dict_counter.update({'key':456})
print(dict_counter)
Copy the code

result:

Counter({'key': 579, '1': 3, '3': 2, '17': 2})
Copy the code

As you can see, the value corresponding to the same key is superimposed with the value corresponding to each update as the final value

3.2 List Situation

dict_counter.update([123])
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 123: 1})
Copy the code
dict_counter.update([123])
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 123: 2})
Copy the code

3.3 String Case

dict_counter.update('update')
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'u': 1, 'p': 1, 'd': 1, 'a': 1, 't': 1, 'e': 1})
Copy the code
dict_counter.update('update')
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'u': 2, 'p': 2, 'd': 2, 'a': 2, 't': 2, 'e': 2})
Copy the code

\

4 substract method

In contrast to the update method, update does addition and substract does subtraction

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2})
Copy the code

4.1 Dictionary Situation

dict_counter.subtract({'key':456})
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'key': -456})
Copy the code
dict_counter.subtract({'key':456})
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'key': -912})
Copy the code

4.2 List Situation

dict_counter.subtract([123])
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 123: -1})
Copy the code
dict_counter.subtract([123])
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 123: -2})
Copy the code

4.3 String Case

dict_counter.subtract('update')
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'u': -1, 'p': -1, 'd': -1, 'a': -1, 't': -1, 'e': -1})
Copy the code
dict_counter.subtract('update')
print(dict_counter)
Copy the code

result:

Counter({'1': 3, '3': 2, '17': 2, 'u': -2, 'p': -2, 'd': -2, 'a': -2, 't': -2, 'e': -2})
Copy the code

Method 5 items

Same as the items method of the dictionary

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print(dict1.items())
print(dict_counter.items())
Copy the code

result:

dict_items([('1', 3), ('3', 2), ('17', 2)])
dict_items([('1', 3), ('3', 2), ('17', 2)])
Copy the code

6. Method of keys

Same as keys method in dictionary

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print(dict1.keys())
print(dict_counter.keys())
Copy the code

result:

dict_keys(['1', '3', '17'])
dict_keys(['1', '3', '17'])
Copy the code

7. Values method

Same as the dictionary values method

dict1 = {"1": 3, "3": 2, "17": 2}
dict_counter = Counter(dict1)
print(dict1.values())
print(dict_counter.values())
Copy the code

result:

dict_values([3, 2, 2])
dict_values([3, 2, 2])
Copy the code