首页 > 解决方案 > python中字典内的列表

问题描述

我目前正在尝试以这种格式将包含列表的字典插入到我的 MongoDB 数据库中:

guildId: myDiscordGuildId,
 questions: [
   'Do you like waffles?',
   'Who put the bop in the bop shoo bop shoo bop?'
 ]

但是,这是一个 Javascript 集合。但是,我使用 python,我不确定如何做到这一点。

我知道如何将列表放入字典中,如下所示:

mydict = {'fruits': ['banana', 'apple', 'orange'],
         'vegetables': ['pepper', 'carrot'], 
         'cheese': ['swiss', 'cheddar', 'brie']}

但是,如果我的列表是这样定义的变量:

我的列表 = []

我如何在字典中使用这个列表变量来获得与上面的水果字典相同的结果?

这个列表是通过我的不和谐机器人操作的,并且被改变了。

当值被硬编码时,我知道如何在字典中使用列表。但是当使用变量时我不确定。

标签: pythonlistdictionary

解决方案


                new_application = {
                    "guildId": ctx.author.id,
                    "questions": questions
                }

固定的


推荐阅读