【问题标题】:how to delete a single specific value within a repl database?如何删除 repl 数据库中的单个特定值?
【发布时间】:2021-12-23 07:49:19
【问题描述】:

我正在使用 repl 的数据库函数制作一个不和谐的 python 机器人,但我不知道如何从数据库中只删除一个值。

@client.command()
async def addvalue(ctx, arg):
 db[str(ctx.author.id)] = [arg]
 await ctx.send("value added")

@client.command()
async def removevalue(ctx, arg):
 db[str(ctx.author.id)] = db[str(ctx.author.id)] - arg #ik this is a dumb way of trying this, i tried like a hundred diff ways nothing works.

【问题讨论】:

    标签: python discord repl.it


    【解决方案1】:

    我要做的是找到要删除的值的索引:

    async def removevalue(ctx, arg):
      c = 0
      for x in db[str(ctx.author.id)]:
        if str(arg) in db[str(ctx.author.id)][c]:
          del db[str(ctx.author.id)][c]
          await ctx.send("Value deleted")
        return
        else:
          c += 1
    

    请注意,我将str() 放在arg 周围,但我很确定那是没用的。我只是想尽一切办法解决它。

    【讨论】:

      【解决方案2】:

      我以前从未使用过它,但从查看example code 看来您可以使用:

      del db[str(ctx.author.id)][index]
      

      the docs 开始,它只是一个键/值存储,因此它的行为似乎与任何 dict 相同。

      【讨论】:

      • 这将删除键的所有值,我希望它只删除一个。
      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2014-08-15
      相关资源
      最近更新 更多