Change language

Python | Find who tweeted the most

| |

Given a list of tweets, the task is to find the user who tweeted the most. If multiple users have the same number of tweets, print all users alphabetically by username.

Python Implementation for finding who tweeted the most

# Write Python3 code here
# collection module used counting in dic for value and keys 
from collections import Counter
  
tweet_names = ["sachin tweet_id_1", 
               "sehwag tweet_id_2",
               "sachin tweet_id_3",
               "sachin tweet_id_4"] 
    
uniq_names = [pref_names.split()[0] for
              pref_names in tweet_names]
  
times = Counter(uniq_names)
repeat = times.values()
  
for element in set(repeat):
    dupl = ([(key, value) for
             key, value in sorted(times.items()) if
             value == element])
      
    if len(dupl) > 1:
        for (key, value) in dupl:
            print (key,’’,value)
    max_value = max(times.values())
    temp_max_result = [(key, value) for 
                       key, value in sorted(times.items()) if
                       value == max_value]
      
    if temp_max_result != dupl:
        for (key,value) in temp_max_result:
            print (key,’’,value)

Output:

sachin  3

Input format:

Read the input from the console.
The first line of input should be the number of test cases
Remaining lines of input should contain each test case input. 
For each test case input:

First-line should contain the number of tweets.
Followed by N lines, each containing the user name
and tweet id separated by a space.

Output format:

Find the user with max number of tweets. 
Print user name and the total number of tweets.

Examples:

Input : 
1
4
sachin tweet_id_1
sehwag tweet_id_2
sachin tweet_id_3
sachin tweet_id_4

Output :
sachin 3

Input :
1
6
sachin tweet_id_1
sehwag tweet_id_2
sachin tweet_id_3
sehwag tweet_id_4
kohli tweet_id_5
kohli tweet_id_6


Output : 
kohli 2
sachin 2
sehwag 2

import.py

import operator

class Twitter:
    
    def __init__(self):
        self.user_tweets_count: list[dict] = []                                 #initialising list that store user tweet count
        self.tweet_users_map: list[dict] = []                                   #initialising list that store user tweet map
        
    def tweets(self):
        tweets_count = int(input(’’))
        tweet_user_map: dict[str, str] = {}
        user_tweet_count: dict[str, str] = {}
        while (tweets_count > 0):
            try:
                tweet=(input(’’))
                user,id=tweet.split(" ")
                if tweet_user_map.get(id,’’)==’’:                                   #check tweet already id exist or not
                    tweet_user_map[id]=user                                         #mapping tweet to corresponding user
                    user_tweet_count[user]=user_tweet_count.setdefault(user,0)+1    #increasing user tweet count
                tweets_count=tweets_count-1
            except Exception:
                print(’Invalid Input. Try again’)

        self.user_tweets_count.append(user_tweet_count)         #adding current user for the particular iteration 
        self.tweet_users_map.append(tweet_user_map)             #adding tweet user mapping particular iteration
    
    def evalTweets(self):
        for item in self.user_tweets_count:
            sort = {key: val for key, val in sorted(item.items(), key = lambda ele: ele[1],reverse = True)}     #Sorting user_tweets_count current iteration data based on value
            maxVal=list(sort.values())[0]                                                                       #Storing the max value
            ret_val: dict[str, str] = {}
            for key,val in sort.items():                                                                        #Loop to filter tweets
                if maxVal==val:                                                                                 #Finding tweets that match max value
                   ret_val[key]=val
            sorted_ret_val={key: val for key, val in sorted(ret_val.items(), key = lambda ele: ele[0])}         #Sorting max value tweets based on name
            for key,val in sorted_ret_val.items():
                print(key,val)                                                                                  #output in each seperate line
    
    def main(self):
        intial_count = int(input(’’))
        while (intial_count > 0):
            try:
                self.tweets()
                intial_count=intial_count-1
            except Exception:
                print(’Invalid Input. Try again’)
        print()
        self.evalTweets()

    
twitter=Twitter()
twitter.main()

How to find the most retweeted tweets

When we think of social media, several questions come to mind. Questions like: Who is the most followed on Twitter? Who tweeted the most about the Oscars? What tweet made the biggest impact? Who sent the most retweet tweets?

This is completely normal, we humans are curious by nature. Finding the most retweeted tweets is also part of this curiosity. Remember that Instagram Egg that got the most likes? Is it ironic that Egg got the most likes and beat Kylie Jenner? Very ironic. Imagine if this happened on Twitter too. Then ask yourself what are the most frequently retweeted tweets? You want to know? That’s the topic of this article: Discovering Popular Tweets. And today in this thread we will talk about the most retweeted tweets and find out how worthy or not worthy of the number of retweets.

If you don’t want to read this article and want to take action, click the Next button. You will be redirected to the Tweet Binder site and simply enter #YourHashtag followed by min_retweets: XXX (xxx is the number you want to enter).

What is Twitter?

First of all. If you haven’t been very active online in the past ten years, you probably don’t know Twitter. This is an unlikely option, but we always take it into account. So what is Twitter? Twitter is a social network where people communicate with each other, post what they think, think what they post, share ideas, news, etc.

What are tweets and retweets?

Twitter wouldn’t be Twitter without tweet. Tweets are messages that you post to Twitter, they are a form of short messages that you can transmit on the platform and which people can respond to. Tweets are usually very short and contain a limited number of characters that you cannot exceed. These tweets can be liked, retweeted and replied to. Retweets are a way to repeat what other users have written. This way, instead of copying and pasting a tweet with an author in one click, you can replay their tweets.

Why do people use Twitter?

The answer is simple: because they can. Why do people use Facebook, Instagram and every other social media they might come across? Because they want and can. Twitter is a terrific social network that anyone can use, be they ordinary people or business people.

Twitter is easy to use, innovative and free! All you have to do is sign up using your email or whatever, and voila, you have a Twitter account that you can now share information with. People also use Twitter because we can follow anyone: artists, singers, newspapers, magazines ... And they also use it because his friends use it and they want to communicate with them on it.

Having a Twitter account means you are up to date with the latest social media events. This will help you stay on top of the latest trends and even connect with new people who may be useful to you in the future. So after signing up, be sure to start following your friends, celebrities, magazines and everything in between, so you can be sure that your Twitter feed will always be full. An empty Twitter feed is boring, and if you ever meet someone you don’t like or want to appear on their feed, you can simply stop following them.

How does a tweet get most retweeted?

Receiving Twitter retweets is cool and fun. But if you are a businessman, then this is vital to your business. Increasing reach and increasing impressions is the goal of every business person on Twitter. Getting more retweets means more exposure and reach, which means more impact on your business and possibly more sales. It’s a simple equation, and that’s why most companies try to use every Twitter trend to get their piece of the pie as well.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method