【发布时间】:2021-06-21 17:10:33
【问题描述】:
在 StackOverflow 上环顾四周,没有找到任何可以回答我问题的东西。 我试图通过这样做来做到这一点
async def leaderboard(self, ctx):
leaderboard = {}
with open("database.json") as f:
leaderboard = json.load(f)
f.close()
sorted_data = {id: money for id, money in sorted(leaderboard.items(), reverse=True ,key=lambda money: money[1])}
print(sorted_data)
我想为我的 Discord 公会制作排行榜,并且我将每个用户的货币存储在 JSON 文件中。
例如,在 JSON 文件中,它们看起来像:
{
"_default": {
"30": {
"money": 0,
"userid": 349846310024642560
},
"32": {
"money": 30861,
"userid": 228718166602153985
},
"33": {
"money": 325663,
"userid": 180688669089202177
}
}
}
我如何排序并抓住 10 个左右最富有的用户?
【问题讨论】:
标签: python json dictionary discord.py