📜  Python|找出发推文最多的人

📅  最后修改于: 2022-05-13 01:54:31.382000             🧑  作者: Mango

Python|找出发推文最多的人

给定推文列表,任务是找到发推文最多的用户。

笔记:
如果多个用户拥有相同数量的推文,则按用户名的字母顺序打印所有用户。

输入格式:

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. 

对于每个测试用例输入:

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

输出格式:

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

例子:

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

代码:用于查找发推文最多的人的Python实现

# 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)
             

输出 :

sachin  3