A simple understanding of the counter structure usage in the Collections module of Python

Source: Internet
Author: User
Tags iterable
The counter data structure is used to provide technical functions. it is similar to the built-in dictionary structure in Python. here we use a few small examples to briefly master the usage of the counter structure in the Python Collections module: counter is a special dictionary, which is mainly used for counting. The key is the item to be counted, and the value is the number.

from collections import Counter>>> c = Counter('hello,world')Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, ',': 1, 'r': 1, 'w': 1})

Three types of parameters can be input during initialization: Dictionary, other iterable data types, and named parameter pairs.

 | __init__(self, iterable=None, **kwds) |  Create a new, empty Counter object. And if given, count elements |  from an input iterable. Or, initialize the count from another mapping |  of elements to their counts. | |  >>> c = Counter()       # a new, empty counter |  >>> c = Counter('gallahad')     # a new counter from an iterable |  >>> c = Counter({'a': 4, 'b': 2})   # a new counter from a mapping |  >>> c = Counter(a=4, b=2)     # a new counter from keyword args

By default, 0 is returned for items that do not exist during access. Counter can be used to count the number of occurrences of certain data, such as the frequency of each number in a long numeric string numbers = "67642192097348921647512014651027586741512651:

>>> C = Counter (numbers) # c stores the frequency of each number> c. most_common () # All numbers are sorted by frequency. If most_common accepts the int parameter n, data of n before the frequency is returned. otherwise, all data will be returned [('1', 8), ('2', 6 ), ('6', 6), ('5', 5), ('4', 5), ('7', 5), ('0', 3 ), ('9', 3), ('8', 2), ('3', 1)]

In addition, you can perform operations such as +,-, min, and max on two Counter objects.

Comprehensive example:

Print ('counter type application') c = Counter ("dengjingdong") # c = Counter ({'n': 3, 'G': 3, 'D ': 2, 'I': 1, 'O': 1, 'E': 1, 'J': 1}) print ("raw data:", c) print ("two elements at most:", c. most_common (2) # print ("number of d:", c ['D']) of the most output elements # print (c. values () # print (sum (c. values () # print (sorted (c. elements () # sort the data in the dictionary in the lexicographically ordered print ('\ n \ n ') "# Delete all d elements del c ['D'] B = Counter (" dengxiaoxiao ") # Use The subtract function to delete elements The number of elements can be negative. C. subtract (B) "You can add data B = Counter (" qinghuabeida ") c. update (B )"""

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.