空集试图为Discord Bot建立字典 [英] Empty set trying to build dictionary for discord bot

查看:116
本文介绍了空集试图为Discord Bot建立字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为不和谐的机器人建立字典.数据结构将如下所示:

I am trying to build a dictionary for a discord bot. The data structure will look like this:

poll = {
  'LINK' : {'MoonRaccoon' : 1, 'TheDirtyTree' : 1},
  'ZRX' : {'MoonRaccoon' : 1, 'Dontcallmeskaface' : 1, 'TheDirtyTree' : 1},  
  'XRP' : {'Dontcallmeskaface' : 1},
  'XLM' : {'aeon' : 1, 'Bob' : 1} 
}

然后将其保存到Repl.it数据库中的db ["poll"]. https://docs.repl.it/misc/database 我猜这只是一本持久词典?(我认为这与我遇到的问题无关)

this will then be saved to db["poll"] in the Repl.it database. https://docs.repl.it/misc/database which i guess is just a persistent dictionary? (I don't think this is relevant to the issue I'm having though)

用户输入的投票方式如下

users will enter their vote like

!v ZRX

然后,漫游器需要添加该投票".到字典.

The bot then needs to add that "vote" to the dictionary.

我想要实现的代码如下:

The code I am trying to get to do that goes like so:

更新:

def update_poll(ticker,requestor):
  vote = {ticker : {requestor : 1}}

for (key, value) in vote.items():
    if 'poll' not in db.keys():
        db['poll'] = []
    if key in db['poll'].keys():
        db['poll'][key].append(value)
    else:
        db['poll'][key] = value

虽然进展不顺利..以前有人建议使用 defaultdict ,但老实说,我还不能将其包裹住.python新手在这里.

it's not going well though.. Some have previously suggested using defaultdict but i honestly have not been able to wrap my brain around it yet. python novice here..

任何建议将不胜感激.

推荐答案

尝试一下:

ticker = 'a'; requestor = 'b'
vote = {ticker : {requestor : 1}}
db = {}
for (key, value) in vote.items():
    if 'poll' not in db.keys():
        db['poll'] = {}
    if key in db['poll'].keys():
        db['poll'][key].append(value)
    else:
        db['poll'][key] = value

这篇关于空集试图为Discord Bot建立字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆