Smart counting of elements using collections.Counter 📊
Forget about manual loops and dictionaries 🚫🔄
When you need to count the frequency of words in a text, the distribution of log types, or popular products in a store, developers usually create an empty dictionary and write a loop with a check if key not in dict: dict[key] = 1. The Counter class takes all this dirty work on itself and makes it as efficient as possible.
— Automatic initialization: You no longer need to check if a key exists in the dictionary. If the element is not there, Counter will not throw a KeyError, but simply return 0. 🛡️
— Finding leaders without sorting: The most_common(k) method returns a list of the k most frequently occurring elements. Under the hood, Python uses optimized heap algorithms, which work much faster than a full dictionary sort via sorted(). 🏆
— Mathematical operations: You can add, subtract, intersect, and merge Counter objects. This turns them into a powerful tool for aggregating metrics and analytics from different data sources in a few lines of code. ➕➖
#Python #DataScience #Coding #Programming #Automation #DevOps
✨ Join Best TG Channels https://xn--r1a.website/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
✅ 13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO
from collections import Counter
# Initial list with duplicate elements
logs = ["error", "info", "error", "warning", "error", "info"]
# 1. Instantly count the number of occurrences
count_dict = Counter(logs)
print(count_dict) # Counter({'error': 3, 'info': 2, 'warning': 1})
# 2. Get the most frequent elements (Top-2)
print(count_dict.most_common(2)) # [('error', 3), ('info', 2)]
# 3. Set math for counters
clicks_day1 = Counter(item=4, banner=2)
clicks_day2 = Counter(item=1, banner=5)
# Combine the results of two days in a single operation
print(clicks_day1 + clicks_day2) # Counter({'banner': 7, 'item': 5})
Forget about manual loops and dictionaries 🚫🔄
When you need to count the frequency of words in a text, the distribution of log types, or popular products in a store, developers usually create an empty dictionary and write a loop with a check if key not in dict: dict[key] = 1. The Counter class takes all this dirty work on itself and makes it as efficient as possible.
— Automatic initialization: You no longer need to check if a key exists in the dictionary. If the element is not there, Counter will not throw a KeyError, but simply return 0. 🛡️
— Finding leaders without sorting: The most_common(k) method returns a list of the k most frequently occurring elements. Under the hood, Python uses optimized heap algorithms, which work much faster than a full dictionary sort via sorted(). 🏆
— Mathematical operations: You can add, subtract, intersect, and merge Counter objects. This turns them into a powerful tool for aggregating metrics and analytics from different data sources in a few lines of code. ➕➖
#Python #DataScience #Coding #Programming #Automation #DevOps
✨ Join Best TG Channels https://xn--r1a.website/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
✅ 13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO
Telegram
AI PYTHON 🌟
You’ve been invited to add the folder “AI PYTHON 🌟”, which includes 14 chats.
❤5🔥1