discord.py - 在列表不和谐python JSON文件中检测相同的项目
问题描述
在 discord PY 中,我有一个 JSON 文件,当我添加一些东西时我会写入该文件。它将它添加到列表中。
@commands.command(pass_context=True)
async def add(self, ctx, itemlist, *, item):
"""Usage: `!add <list> <item>`"""
with open("list.json", "r") as f:
data = json.load(f)
try:
data[itemlist].append(item)
except KeyError:
data[itemlist] = [item]
with open("list.json", "w") as f:
json.dump(data, f)
await ctx.send("Succesfully added '{}' to the list '{}'".format(item, itemlist))
我想知道如何检测列表中是否已经存在相同的项目,然后以一种有效的方式添加重复项的数量。
例如,如果有人添加了一个苹果: !add list apple 当你查看列表时,它会说:apple
现在,当有人再次 !add list apple 时,它会改为显示 apple x2
解决方案
推荐阅读
- github - Jenkinsfile - 如何访问其他 github 文件?
- terraform - 具有可变对象的 Terraform 条件动态块
- r - 如何修复R中的特殊字符?替换字符
- python - 启动 selenium chromedriver 时出现问题:引发错误
- r - R函数返回0,在函数外运行时相同函数内的代码返回正确值
- r - 修改数据框R中的案例顺序
- reactjs - 为什么 Material UI Card Header 中的这个按钮不做任何事情?
- javascript - 在 React native + expo 中获取抛出网络请求失败
- javascript - 在反应打字稿渲染中循环枚举会产生错误
- python - Python程序查找给定整数上下5个素数