How to implement Statistics by using collections.Counter method in python
Editor to share with you how python uses the collections.Counter method to achieve statistics, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Collections.Counter method
Counter is a container object, and the statistics of hash objects can be realized by using the Counter class in the collections module.
Counter is an unordered container type that is stored as a dictionary key-value pair, where the element is key and its count is value.
The count value can be any Interger (including 0 and negative numbers).
The Counter () object also has several callable methods:
Most_common (n)-- TOP n elements with the highest frequency
Elements-get all the keys converted through list
Update-add object
Subtrct-Delete object
Subscript access a ['xx']-returns 0 if it does not exist
Import collectionsc = collections.Counter ('helloworld')
Directly display the frequency of each element
Print (c) # Counter ({'lump: 3,' oasis: 2, 'hype: 1,' eBay: 1, 'wicked: 1,' ritual: 1, 'dudes: 1})
Use most_common to display the most n elements
When multiple elements have the same count value, the arrangement is in no definite order.
Print (c.most_common (3)) # [('lags, 3), (' oaks, 2), ('hacks, 1)]
Get it using an array subscript, similar to a dictionary:
Print ("The number of'okeeper:", c ['o']) # The number of'okeeper: 2
Statistical list: (as long as the objects in the list can be hashed)
Import collectionsx = [1dictc 2, 2: 1, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1] print (c.most_common (3)) # [(8, 2), (1, 2)] dictc = dict (c) # convert to dictionary print (dictc) # {1: 2, 2: 1 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 4}
If there are unhashalbe objects in the list, for example, a mutable list, it cannot be counted.
Tuples can also be counted.
C = collections.Counter ([[1Mague 2], "hello", 123,0.52]) # TypeError: unhashable type: 'list'
After you get the Counter counter object, you can also make incremental updates based on it.
Elements ()-returns the iterator
Elements are arranged in no definite order, and elements with a number less than 1 are not included.
A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import collectionsc = collections.Counter (Apocalypse 4, Bethle4, Benz2, print, 1) print (c) # Counter ({'axie: 4,' baked: 2, 'cedar: 1}) list (c.elements ()) # [' await, 'baked,' axed, 'baked,' c']
Subtract function-- subtract elements
Import collectionsc = collections.Counter (["a", "b", "c", "a"]) print (c) # Counter ({'aqu: 2,' baked: 1, 'croup: 1}) print (list (c.elements () # expand # [' a','a', 'baked,' c'] # reduce element c.subtract (["a", "b"]) print (c) # Counter ({'aqu: 1) 'cased: 1,' baked: 0}) print (list (c.elements () # ['averse,' c']
Update function-- add elements
The update function is very useful when doing incremental counting.
A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import collectionsc = collections.Counter (["a", "b", "c", "a"]) print (c) # Counter ({'aqu: 2,' baked: 1, 'croup: 1}) print (list (c.elements () # expand # [' a', 'baked,' baked,'c'] c.update (["a", "d"]) print (c) # Counter ({'averse: 3,' baked: 1) ) print (list (c.elements () # ['await,' averse, 'baked,' clocked,'d']
Del function-delete key
When the count value is 0, it does not mean that the element is deleted, and the deleted element should use del.
Import collectionsc = collections.Counter ('helloworld') print (c) # Counter ({' lump: 3, 'oval: 2,' hype: 1, 'eBay: 1,' wicked: 1, 'ritual: 1}) c ["d"] = 0print (c) # Counter ({' lump: 3, 'oval: 2,' hinge: 1, 'ethereal: 1,' wicked: 1, 'ritual: 1 (del: 0}) del c ["l"] print (c) # Counter ({'oints: 2,' hackers: 1, 'eBay: 1,' wicked: 1, 'ringing: 1,' dudes: 0}) these are all the contents of the article "how python uses the collections.Counter method to implement Statistics" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!