1

What I want to accomplish is to calculate the ranking of the most active users in a group. My approach to it was to first iterate through all message history in a group, get the user name, assign the message to it and then sort it in python from highest (most often sent from a given user) to lowest, but I have no idea how to make it programmatically and I've been stuck in this code:

async def calculate_ranking(event):
@client.on(events.NewMessage)
async def calculate_ranking(event):
    messages = {}
    if "!ranking" in event.raw_text:
        chat_id = event.chat_id
        async for message in client.iter_messages(chat_id, reverse=True):
            author = message.sender.username
            messages[author] += 1
            print(messages)

I have no idea on how to assign a message to a user so the output would be as for example:

  1. user1: 12 messages
  2. user2: 10 messages
shikim0ri
  • 13
  • 2
  • Does this answer your question? [Item frequency count in Python](https://stackoverflow.com/questions/893417/item-frequency-count-in-python) – Florian Fasmeyer Jun 25 '21 at 13:57

1 Answers1

0

For the rank itself, you can use collections.Counter()

import collections
collections.Counter(['jon', 'alan', 'jon', 'alan', 'bob'])
# Output: Counter({'jon': 2, 'alan': 2, 'bob': 1})

So all you have left to do is to get the username.

# Your job! :)
data = dict(
    username = ['jon',    'alan',     'jon',                'alan',   'bob'],
    message =  ['hello!', 'hey jon!', 'How are you doing?', 'Im fine', 'hola!']
)
Counter(data.username)
Florian Fasmeyer
  • 795
  • 5
  • 18
  • 1
    problem solved, this example made it clear for me, thanks! – shikim0ri Jun 25 '21 at 13:49
  • Since it's a dup (how to get rank/frequencies) I'll have to mark it as a dup. But I am pleased I could be of help. ;) note: "we love (some) dupes" https://stackoverflow.com/help/duplicates – Florian Fasmeyer Jun 25 '21 at 13:54