Python's counter class: computes the most frequently occurring element in a sequence

Source: Internet
Author: User

Counter class: Computes the most frequently occurring element in a sequence

1  fromCollectionsImportCounter2 3c = Counter ('ABCDEFADDFFCCEF')4 Print('The complete Counter object:', c)5 6A_times = c['a']7 Print('number of occurrences of element a:', A_times)8 9C_most = C.most_common (3)Ten Print('the three most frequently occurring elements:', C_most) One  ATimes_dict =c.values () - Print('a list of the number of elements that appear:', Times_dict) -  theTotal_times =sum (c.values ()) - Print("sum of occurrences of all elements:", Total_times)

Operation Result:

1Full Counter object: Counter ({'F': 4,'C': 3,'D': 3,'a': 2,'e': 2,'b': 1})2Number of occurrences of element a: 23The three most frequently occurring elements: [('F', 4), ('C', 3), ('D', 3)]4List of occurrences of each element: Dict_values ([2, 1, 3, 3, 2, 4])5Sum of occurrences of all elements: 15

Python's counter class: computes the most frequently occurring element in a sequence

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.