在字典的典型键中添加新值 [英] adding new values in a typical key in a dictionary

查看:488
本文介绍了在字典的典型键中添加新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为另一个字典的值添加相同的键,如下所示?

how do I add value to another dictionary to the same key like below

con = {'a':{'b':'c'}, b:{'d':'e'}} 

进入

con = {'a':{'b':'c', 'e':'f'}, b:{'d':'e'}} 

推荐答案

在当前设置下,它始终是一个键和一个值对,因此它将是key:value

With the current set up, its always a key and a value pair, so it will be key:value

如果您希望一个键具有多个值,请使用.

If you would like to have more than one value to a key, please use.

from collections import defaultdict
myDict = defaultdict(list)

现在,您可以为键添加多个值.

Now, you can add more than one value to the key.

myDict[key1].append(keyA:Value)
myDict[key1].append(keyB:Value)

希望这会有所帮助.

干杯!

这篇关于在字典的典型键中添加新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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