Python 如何附加到词典中

Python 如何附加到词典中,python,dictionary,Python,Dictionary,我正在尝试创建这样一个词典词典: food = {"Broccoli": {"Taste": "Bad", "Smell": "Bad"}, "Strawberry": {"Taste": "Good", "Smell": "Good"}} nutCol = [i[0] for i in result.description] 但我是从SQL表填充它的。因此,我将SQL表拉入一个名为“result”的SQL对象中。然后我得到了这样的列名: food = {"Broccoli"

我正在尝试创建这样一个词典词典:

food = {"Broccoli": {"Taste": "Bad", "Smell": "Bad"},
        "Strawberry": {"Taste": "Good", "Smell": "Good"}}
nutCol = [i[0] for i in result.description]
但我是从SQL表填充它的。因此,我将SQL表拉入一个名为“result”的SQL对象中。然后我得到了这样的列名:

food = {"Broccoli": {"Taste": "Bad", "Smell": "Bad"},
        "Strawberry": {"Taste": "Good", "Smell": "Good"}}
nutCol = [i[0] for i in result.description]
这张桌子大约有40个特征,所以它很长

我可以做到这一点

foodList = {}
for id, food in enumerate(result):
    addMe = {str(food[1]): {nutCol[id + 2]: food[2], nulCol[idx + 3]: 
            food[3] ...}}
    foodList.update(addMe)
但这当然看起来很可怕,需要一段时间来写。我仍在研究如何构建这整件事,所以我可能需要对它进行几次修改……这可能会变得非常乏味

有没有一种干燥的方法可以做到这一点?

addMe={str(食物[1]):dict(zip(nutCol[2:],食物[2:])}

zip
将获取两个(或更多)项目列表并对元素进行配对,然后您可以将结果传递到
dict
以将配对转换为字典。

addMe={str(food[1]):dict(nutCol[2:],food[2:])


zip
将获取两个(或更多)项目列表并对元素进行配对,然后您可以将结果传递到
dict
以将配对转换为字典。

假设列0是您希望用作键的,并且您确实希望构建字典字典,则其:

detail_names = [col[0] for col in result.description[1:]]
foodList = {row[0]: dict(zip(detail_names, row[1:]))
            for row in result}
foodList = {row[k]: {col[0]: row[i]
                     for i, col in enumerate(result.description) if i != k}
            for row in result}
概括而言,如果列
k
是您的标识,则其:

detail_names = [col[0] for col in result.description[1:]]
foodList = {row[0]: dict(zip(detail_names, row[1:]))
            for row in result}
foodList = {row[k]: {col[0]: row[i]
                     for i, col in enumerate(result.description) if i != k}
            for row in result}

(此处每个子字典都是除列
k
以外的所有列)

假设列0是您希望用作键的,并且您确实希望构建字典字典,则其:

detail_names = [col[0] for col in result.description[1:]]
foodList = {row[0]: dict(zip(detail_names, row[1:]))
            for row in result}
foodList = {row[k]: {col[0]: row[i]
                     for i, col in enumerate(result.description) if i != k}
            for row in result}
概括而言,如果列
k
是您的标识,则其:

detail_names = [col[0] for col in result.description[1:]]
foodList = {row[0]: dict(zip(detail_names, row[1:]))
            for row in result}
foodList = {row[k]: {col[0]: row[i]
                     for i, col in enumerate(result.description) if i != k}
            for row in result}

(这里每个子字典都是除列
k
之外的所有列)

为了使解决方案位置独立,您可以使用
dict1.update(dict2)
。这只是将dict2与dict1合并

在我们的例子中,由于我们有dict of dict,我们可以使用
dict['key']
作为dict1,只需添加任何额外的键、值对作为dict2

这里有一个例子

food = {"Broccoli": {"Taste": "Bad", "Smell": "Bad"}, 
        "Strawberry": {"Taste": "Good", "Smell": "Good"}}

addthis = {'foo':'bar'}
假设您想添加
addthis
dict到
food['草莓]
,我们可以简单地使用

food["Strawberry"].update(addthis)
获得结果:

>>> food
{'Strawberry': {'Taste': 'Good', 'foo': 'bar', 'Smell': 'Good'},'Broccoli': {'Taste': 'Bad', 'Smell': 'Bad'}}
>>>  

为了使溶液位置独立,您可以使用
dict1.update(dict2)
。这只是将dict2与dict1合并

在我们的例子中,由于我们有dict of dict,我们可以使用
dict['key']
作为dict1,只需添加任何额外的键、值对作为dict2

这里有一个例子

food = {"Broccoli": {"Taste": "Bad", "Smell": "Bad"}, 
        "Strawberry": {"Taste": "Good", "Smell": "Good"}}

addthis = {'foo':'bar'}
假设您想添加
addthis
dict到
food['草莓]
,我们可以简单地使用

food["Strawberry"].update(addthis)
获得结果:

>>> food
{'Strawberry': {'Taste': 'Good', 'foo': 'bar', 'Smell': 'Good'},'Broccoli': {'Taste': 'Bad', 'Smell': 'Bad'}}
>>>  

瞧,这对我来说不太管用,因为这是一本字典词典。这不仅仅是一次添加一个密钥/对。我需要能够说一些像foodList[“Chicken”][“Protein”]这样的话,你建议的代码并不是在制作字典词典。。。您是否认为有一个特定的字段是“id”,您希望将其他属性与之关联?请参阅:这对我来说不太合适,因为它是一个字典字典。这不仅仅是一次添加一个密钥/对。我需要能够说一些像foodList[“Chicken”][“Protein”]这样的话,你建议的代码并不是在制作字典词典。。。是否有一个您认为是“id”的特定字段要与其他属性关联?