python - 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?
解决方案
使用sorted()
:
for guild in sorted(client.guilds, key=lambda guild: guild.member_count, reverse=True):
print(f'Server Name: {guild}, Members: {guild.member_count}')
推荐阅读
- sqlite - NativeScript:SQLCipher 不接受 PBKDF2 派生密钥作为有效字符串
- python - 矩阵乘以 Keras 中的矩阵列表
- java - 违反里氏替换原则的策略模式
- html - optgroup 与我的网站联系人合作时出现问题,optgroup 是否有效?我应该用什么?
- microsoft-graph-api - OneDrive - 使用 Rest API 访问由共享 webURL 指定的文件夹
- javascript - 如何根据 Express 参数检索 Mongoose 模型
- html - Firefox 的 SVG 文本路径问题
- html - 带边框折叠的css浮动
- python - 如何检查元素包含在哪个列表中
- mercurial - mercurial extensions:高效的模板关键字实现