Counters in Python | Set 2 (Accessing Counters) - GeeksforGeeks (2024)

Last Updated : 06 Sep, 2023

Summarize

Comments

Improve

Counters in Python | Set 1 (Initialization and Updation)

Counters in Python | Set 2

Once initialized, counters are accessed just like dictionaries. Also, it does not raise the KeyValue error (if key is not present) instead the value’s count is shown as 0.

Example: In this example, we are using Counter to print the key and frequency of that key. The elements present inside the frequency map are printed along with their frequency and if the element is not present inside the Counter map then the element will be printed along with 0.

Python3

from collections import Counter

# Create a list

z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']

col_count = Counter(z)

print(col_count)

col = ['blue','red','yellow','green']

# Here green is not in col_count

# so count of green will be zero

for color in col:

print (color, col_count[color])


Output: <

Counter({‘blue’: 3, ‘red’: 2, ‘yellow’: 1})blue 3red 2yellow 1green 0

elements() method of Counter in Python

The elements() method returns an iterator that produces all of the items known to the Counter. Note: Elements with count <= 0 are not included.

Example : In this example, the elements inside the Counter would be printed by using the elements() method of Counter.

Python3

# Python example to demonstrate elements()

from collections import Counter

coun = Counter(a=1, b=2, c=3)

print(coun)

print(list(coun.elements()))


Output :

Counter({'c': 3, 'b': 2, 'a': 1})['a', 'b', 'b', 'c', 'c', 'c']

most_common() method of Counter in Python

most_common() is used to produce a sequence of the n most frequently encountered input values and their respective counts. If the parameter ‘n’ is not specified or None is passed as the parametermost_common()returns a list of all elements and their counts.

Example: In this example, the element with the most frequency is printed followed by the next-most frequent element by using most_common() method inside Counter in Python.

Python3

from collections import Counter

coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219)

# This prints 3 most frequent characters

for letter, count in coun.most_common(3):

print('%s: %d' % (letter, count))


Output :

f: 219
d: 120
c: 3


Counters in Python | Set 2 (Accessing Counters) - GeeksforGeeks (1)

GeeksforGeeks

Counters in Python | Set 2 (Accessing Counters) - GeeksforGeeks (2)

Improve

Please Login to comment...

Counters in Python | Set 2 (Accessing Counters) - GeeksforGeeks (2024)
Top Articles
Ash Legends pick rates | Apex Legends Status
Average long-term US mortgage rate climbs above 7% to highest level since late November
417-990-0201
855-392-7812
Froedtert Billing Phone Number
Euro (EUR), aktuální kurzy měn
Blanchard St Denis Funeral Home Obituaries
Craigslist Cars And Trucks For Sale By Owner Indianapolis
My Boyfriend Has No Money And I Pay For Everything
What Auto Parts Stores Are Open
Jasmine
Paketshops | PAKET.net
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Boat Jumping Female Otezla Commercial Actress
Nexus Crossword Puzzle Solver
Goldsboro Daily News Obituaries
World History Kazwire
The Murdoch succession drama kicks off this week. Here's everything you need to know
Overton Funeral Home Waterloo Iowa
Gemita Alvarez Desnuda
Toy Story 3 Animation Screencaps
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Acts 16 Nkjv
Beverage Lyons Funeral Home Obituaries
8005607994
Essence Healthcare Otc 2023 Catalog
Watson 853 White Oval
Feathers
Maisons près d'une ville - Štanga - Location de vacances à proximité d'une ville - Štanga | Résultats 201
Spirited Showtimes Near Marcus Twin Creek Cinema
Bj's Tires Near Me
Ff14 Sage Stat Priority
Elanco Rebates.com 2022
O'reilly's Wrens Georgia
3 Bedroom 1 Bath House For Sale
The 50 Best Albums of 2023
AI-Powered Free Online Flashcards for Studying | Kahoot!
Tugboat Information
More News, Rumors and Opinions Tuesday PM 7-9-2024 — Dinar Recaps
What Is A K 56 Pink Pill?
Owa Hilton Email
Panolian Batesville Ms Obituaries 2022
Natasha Tosini Bikini
Garland County Mugshots Today
My Eschedule Greatpeople Me
Studentvue Calexico
9:00 A.m. Cdt
Sherwin Source Intranet
Blippi Park Carlsbad
Raley Scrubs - Midtown
San Pedro Sula To Miami Google Flights
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5796

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.