首页 > 解决方案 > How to sort a list from ascending to decsending discord.py

问题描述

   def ext():
       for filename in os.listdir('./cogs'):
           if filename.endswith('.py'):
               client.load_extension(f'cogs.{filename[:-3]}')
               print(f'{filename} was loaded successfully!')
       print('------------------------------------')
       for guild in client.guilds:
           print(f'Server Name: {guild}, Members: {guild.member_count}')
       print('------------------------------------')

This is my extension function the runs on_ready. It prints out all of the servers the bot is in. I am wondering if there is a way to make it show from most members to least?

标签: pythonpython-3.xsortingdiscord.pybots

解决方案


使用sorted()

for guild in sorted(client.guilds, key=lambda guild: guild.member_count, reverse=True):
    print(f'Server Name: {guild}, Members: {guild.member_count}')

推荐阅读