【发布时间】:2021-06-23 12:29:20
【问题描述】:
我正在开发一个 discord.py 机器人,当我尝试对每个用户的余额进行“最高余额”命令排序时,如果键有 3 位数字,它将停止正确排序。
# bot and discord are defined above here, btw
@bot.command(name='top')
async def top(context):
scores.read("brewscores.ini")
tops = scores['scores']
print('hi\n', tops)
print('sotred')
from collections import Counter #todo : move this somewhere else
c = Counter(tops)
a = c.most_common(5) #//https://stackoverflow.com/a/40496562/9654083
string = """"""
await context.send("Loading balancers...")
for item in a:
print(item)
g, s = item
string += (f"{g}: {s}\n")
em = discord.Embed(title="Top 5 Balancers", description=f'The top 5 contestants are!:{string}')
await context.send(embed=em)
分数:
[scores]
placeholder = 0
(username censored for privacy) = 35
(username censored for privacy) = 49
No other balancers! = 0
You can stop reading now... = 0
rats#3234 = 100
运行命令时输出我和所有者进入 Discord:
(censored for privacy): 49
(censored for privacy): 35
rats#3234: 100
placeholder: 0
you can stop reading now...: 0
我们期待的:
rats#3234: 100
(censored for privacy): 49
(censored for privacy): 35
placeholder: 0
you can stop reading now...: 0
请注意“rats#3234”是如何放错位置的。 我们没有在 99 或 3 等 2 位或 1 位数字中看到这种行为。为什么会发生这种情况?
【问题讨论】:
-
您能否提供一个包含
tops内容的sn-p,该内容可以按您的预期工作,而另一个则不能?您如何将分数与名称分开?你在数什么? -
我编辑了问题,现在可以回答您的问题了吗?
-
不完全是:您想根据分数对文件的行进行排序吗?阅读
ini文件后,scores和tops里面是什么?更重要的是重现您的代码,scores是什么? -
scores是读取的ini文件。tops是scores的scores部分的内容。我其实有办法,我现在就给出答案。 -
好酷,顺便说一句,我的意思是
scores.read("brewscores.ini"):你在哪里定义scores?那是什么python对象?
标签: python python-3.x discord.py