如何在python中组合两个for in循环的输出?



这是一个不和谐机器人的脚本,它获取频道中有反应的所有消息,检查它是否有你正在寻找的反应(msgSplit[1])并检查它是否有你正在寻找的反应数量与if num >= int(msgSplit[2]):

async for historical_message in message.channel.history(limit=historyLimit):
for reaction in historical_message.reactions:
if reaction.emoji == msgSplit[1]:
num = reaction.count
if num >= int(msgSplit[2]):
print(reaction, num)

由于频道历史包含5条带有我们正在寻找的特定表情符号反应的消息,我们得到5个输出。

reaction  2 
reaction  1 
reaction  1 
reaction  2 
reaction  3

我如何从上到下对这些输出进行排序,以得到这样的输出:

reaction  3 
reaction  2 
reaction  2 
reaction  1 
reaction  1 

像这样:

async for historical_message in message.channel.history(limit=historyLimit):
reactions = []                                # Create list
for reaction in historical_message.reactions:
if reaction.emoji == msgSplit[1]:
num = reaction.count
if num >= int(msgSplit[2]):
reactions.append((reaction, num)) # Append to the list
reactions.sort(key=lambda tup: tup[1])        # Sort it
for reaction in reactions:                    # And then print
print(reaction)

最新更新