【问题标题】:How do I create a ranking of telegram group如何创建电报组的排名
【发布时间】:2021-09-08 21:24:42
【问题描述】:

我想要完成的是计算组中最活跃用户的排名。 我的方法是首先遍历组中的所有消息历史记录,获取用户名,将消息分配给它,然后在 python 中从最高(最常从给定用户发送)到最低排序,但我没有想法如何以编程方式制作它,我一直被困在这段代码中:

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)

我不知道如何将消息分配给用户 所以输出例如:

  1. user1:12 条消息
  2. user2:10 条消息

【问题讨论】:

标签: python bots telegram telethon


【解决方案1】:

对于排名本身,您可以使用 collections.Counter()

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

所以你剩下要做的就是获取用户名。

# Your job! :)
data = dict(
    username = ['jon',    'alan',     'jon',                'alan',   'bob'],
    message =  ['hello!', 'hey jon!', 'How are you doing?', 'Im fine', 'hola!']
)
Counter(data.username)

【讨论】:

  • 问题解决了,这个例子让我明白了,谢谢!
  • 既然是 dup(如何获得排名/频率),我必须将其标记为 dup。但我很高兴我能帮上忙。 ;) 注意:“我们喜欢(某些)骗子”stackoverflow.com/help/duplicates
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 2023-01-19
  • 2020-01-03
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多