Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find number of likes and dislikes?
The ability to analyze likes and dislikes on social media platforms is essential for understanding user engagement and sentiment. As a Python programmer, you'll often need to count these interactions to gauge content performance and user preferences.
In this article, we'll explore two practical methods for counting likes and dislikes in Python datasets using different approaches.
Approaches
To count likes and dislikes in Python, we can use two effective methods ?
Using the Counter class from collections
Using manual loop iteration
Let's examine both approaches with complete examples.
Using Counter from Collections
The Counter class from Python's collections module provides an efficient way to count occurrences of elements in any iterable. It creates a dictionary-like object where items are keys and their counts are values.
Example
from collections import Counter
def count_likes_dislikes(reactions):
counter = Counter(reactions)
likes = counter.get('like', 0)
dislikes = counter.get('dislike', 0)
return likes, dislikes
# Example usage
post_reactions = ['like', 'like', 'dislike', 'like', 'dislike', 'like']
likes, dislikes = count_likes_dislikes(post_reactions)
print(f"Likes: {likes}")
print(f"Dislikes: {dislikes}")
print(f"Total reactions: {likes + dislikes}")
Likes: 4 Dislikes: 2 Total reactions: 6
Using Loop Iteration
Manual iteration gives you more control over the counting process and allows for additional logic during the iteration. This approach is useful when you need custom conditions or data processing.
Example
def count_likes_dislikes_manual(reactions):
likes = 0
dislikes = 0
for reaction in reactions:
if reaction == 'like':
likes += 1
elif reaction == 'dislike':
dislikes += 1
return likes, dislikes
# Example usage
post_reactions = ['like', 'like', 'dislike', 'like', 'dislike', 'like']
likes, dislikes = count_likes_dislikes_manual(post_reactions)
print(f"Likes: {likes}")
print(f"Dislikes: {dislikes}")
print(f"Engagement ratio: {likes/(likes+dislikes)*100:.1f}% positive")
Likes: 4 Dislikes: 2 Engagement ratio: 66.7% positive
Real-World Example
Here's a more practical example that processes multiple posts and calculates engagement statistics ?
from collections import Counter
def analyze_post_engagement(posts_data):
results = []
for post_id, reactions in posts_data.items():
counter = Counter(reactions)
likes = counter.get('like', 0)
dislikes = counter.get('dislike', 0)
total = likes + dislikes
engagement_score = (likes - dislikes) / total if total > 0 else 0
results.append({
'post_id': post_id,
'likes': likes,
'dislikes': dislikes,
'total_reactions': total,
'engagement_score': round(engagement_score, 2)
})
return results
# Sample data
social_media_data = {
'post_1': ['like', 'like', 'like', 'dislike', 'like'],
'post_2': ['dislike', 'dislike', 'like', 'dislike'],
'post_3': ['like', 'like', 'like', 'like', 'like']
}
analysis = analyze_post_engagement(social_media_data)
for post in analysis:
print(f"Post {post['post_id']}: {post['likes']} likes, {post['dislikes']} dislikes")
print(f"Engagement Score: {post['engagement_score']}")
print("-" * 30)
Post post_1: 4 likes, 1 dislikes Engagement Score: 0.6 ------------------------------ Post post_2: 1 likes, 3 dislikes Engagement Score: -0.5 ------------------------------ Post post_3: 5 likes, 0 dislikes Engagement Score: 1.0 ------------------------------
Comparison
| Method | Code Length | Performance | Best For |
|---|---|---|---|
| Counter | Shorter | Faster | Simple counting tasks |
| Loop Iteration | Longer | Good | Custom logic and conditions |
Conclusion
Use Counter for simple, efficient counting of likes and dislikes. Choose manual iteration when you need custom logic or additional processing during the counting process.
