如何创建电报组排名
How do I create a ranking of telegram group
我想完成的是计算一个组中最活跃用户的排名。
我的方法是首先遍历一个组中的所有消息历史记录,获取用户名,将消息分配给它,然后在 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:12 条消息
- 用户 2:10 条消息
对于排名本身,您可以使用 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)
我想完成的是计算一个组中最活跃用户的排名。 我的方法是首先遍历一个组中的所有消息历史记录,获取用户名,将消息分配给它,然后在 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:12 条消息
- 用户 2:10 条消息
对于排名本身,您可以使用 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)