【问题标题】:collections.Counter.most_common stops working correctly in 3 digit numberscollections.Counter.most_common 在 3 位数字中停止正常工作
【发布时间】: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 文件后,scorestops 里面是什么?更重要的是重现您的代码,scores 是什么?
  • scores 是读取的ini文件。 topsscoresscores 部分的内容。我其实有办法,我现在就给出答案。
  • 好酷,顺便说一句,我的意思是scores.read("brewscores.ini"):你在哪里定义scores?那是什么python对象?

标签: python python-3.x discord.py


【解决方案1】:

不需要Counter:

scores.read("brewscores.ini")
print(scores)
tops = scores["scores"]
sortedtopkeys = sorted(tops, key=lambda k: int(tops[k]), reverse=True)
print(sortedtopkeys)

这样你就可以根据值对dict进行排序,如the answer you have in the comment所示,但将值解释为int。

干杯!

【讨论】:

    【解决方案2】:

    更新

    如果您不关心此错误发生原因的详细信息,请参阅https://stackoverflow.com/a/66821488/9654083 以获得更好的答案。 imo这个答案是该答案的最佳补充,而不是替代。

    所以事实证明,当它是一个字符串时,collections.Counter.most_common 首先查看第一个数字以找出哪个更大,然后是第二个,然后是第三个。不是全部一起。

    也就是说,这个:

    [scores]
    placeholder = 0
    (censored) = 35
    (censored) = 49
    rats#3234 = 9
    

    将老鼠放在第一位,因为它认为它是

    [scores]
    placeholder = 0
    (censored) = 3
    (censored) = 4
    rats#3234 = 9
    

    如果有两个相同的第一个数字,它将继续比较第一个和第二个数字:

    [scores]
    placeholder = 0
    (censored for privacy) = 35
    (censored) = 49
    

    而不仅仅是第一个数字,等等。

    这意味着与我们的想法相反,这不仅仅是因为它是 3 位数。这是因为它的第一位数字最大。

    其他开发人员发现了这一点,因为 9 是老鼠的值会输出

    rats#3234: 9
    (censored): 49
    (censored): 35
    placeholder: 0
    

    这意味着我们必须在排序前转换为 int。

    在我测试时给我一秒钟。

    【讨论】:

    • 尝试将值转换为int
    • 我已经更新了答案。我会在几分钟内测试。
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2015-01-07
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多