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