Python program to find number of likes and dislikes?


The ability to communicate our ideas, feelings, and preferences through likes and dislikes on social media platforms has revolutionized the way we connect with and interact with material. These straightforward yet effective indications are crucial for determining the level of interest and sentiment surrounding a post, video, or any other type of shared content. You could frequently get across situations as a Python programmer where you need to examine the number of likes and dislikes on a given article or video to learn more about user preferences or gauge engagement levels.

We will study two distinct methods for determining the number of likes and dislikes in each dataset as we dig into the world of Python programming in this post.

Approaches

To search for a minimum number of rotations to obtain an actual string in Python, we can follow the two methods −

  • Utilizing the Counter.

  • Utilizing the Loop Iteration.

Let us investigate both approaches −

Approach 1: Utilizing the Counter

A robust and effective method for counting the appearances of components in each iterable is the Counter class from Python's collections module. We can quickly count the number of likes and dislikes in a dataset using the Counter class without laborious manual iterations.

The Counter class effectively constructs an object that resembles a dictionary, with the iterable's items serving as keys and their corresponding counts serving as values. Due to the lack of explicit iteration and counting logic, it is the perfect tool for counting occurrences.

Algorithm

The steps to count likes and dislikes in Python are as follows −

Step 1 − From the collection’s module the Counter class ought to be imported.

Step 2 − Create a function that accepts a list of likes and dislikes as input and calls it count_likes_dislikes.

Step 3 − By giving the list of preferences to the Counter class, you may create a counter object.

Step 4 − Utilize the get() function of the counter object to get the total number of likes and dislikes.

Step 5 − The likes and dislikes counts are returned.

Example

from collections import Counter

def count_likes_dislikes(likes_dislikes):
   counter = Counter(likes_dislikes)
   likes = counter.get('like', 0)
   dislikes = counter.get('dislike', 0)
   return likes, dislikes

# Example usage:
post_likes_dislikes = ['like', 'like', 'dislike', 'like', 'dislike']
likes, dislikes = count_likes_dislikes(post_likes_dislikes)
print("Likes:", likes)
print("Dislikes:", dislikes)

Output

Likes: 3
Dislikes: 2

Approach 2: Utilizing the Efficient Approach

Manually iterating through the dataset can be an effective strategy in circumstances where you may need more control over the counting process or need to carry out extra actions while counting. We may explicitly check for specified values, update counters accordingly, and take care of any additional processing, if necessary, by iterating over each element in a loop.

This method offers more customization and versatility because you can include additional conditions or logic during the iteration phase. When special criteria or data manipulation is involved, it offers a simple solution even if it might take more lines of code than using a Counter.

Algorithm

The steps to count likes and dislikes in Python are as follows −

Step-1: Create a function that accepts a list of likes and dislikes as input and calls it count_likes_dislikes. Step-2: Put the likes and dislikes variables to 0. Step-3: Go through the list's elements iteratively. Step-4: Utilize an if statement to determine whether the element is "like" or "dislike." Step-5: Add one to the likes variable if the element is marked as "like." Step-6: Add one to the dislikes variable if the element is "dislike." Step-7: The likes and dislikes counts are returned.

Example

#create a user-defined function
def count_likes_dislikes(likes_dislikes):
   likes = 0
   dislikes = 0
   for item in likes_dislikes:
      if item == 'like':
         likes += 1
      elif item == 'dislike':
         dislikes += 1
   return likes, dislikes

# Example usage:
post_likes_dislikes = ['like', 'like', 'dislike', 'like', 'dislike']
likes, dislikes = count_likes_dislikes(post_likes_dislikes)
print("Likes:", likes)
print("Dislikes:", dislikes)

Output

Likes: 3
Dislikes: 2

Conclusion

In this article, we looked at two methods for counting Python users' likes and dislikes. The first method made use of the Collections module's Counter class, which offered a clear mechanism to count the appearances of components. The second method included manually going over the list iteratively, giving the user more control over the counting procedure. The decision between the two strategies depends on the needs of your project, and both are efficient. Understanding these techniques will make it simple for you to use Python to analyze and extract insightful information from social media data.

Updated on: 28-Jul-2023

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements