在python中添加了多个dict

时间:2017-10-23 06:10:20

标签: python dictionary

我有这样的6个字典

dict1
dict2
dict3
dict4
dict5
dict6

现在我希望所有这一切都在一个字典中。所以我用了这个

dict1.update({'dict2':dict2}) 
dict3.update({'dict1':dict1})
dict4.update({'dict4':dict3})
dict5.update({'dict5':dict4})                           
dict6.update({'dict6':dict5})

最后 dict6 包含所有值,但格式不正确且不是 pythonic 的方法它

我想改进这个建议

现在我变得像这样,但我不想这样

{"main_responses": {"dict1": {"dict2": {"dict3": {"dict4": {"dict5": {}}}}}}}

我想要

{"main_responses":{ "dict1": {dict1_values}, "dict2": {dict2_values}..... and so on

6 个答案:

答案 0 :(得分:2)

试试这个:

from itertools import chain
d = chain.from_iterable(d.items() for d in (ada_dict, 
                                        wordpress_version_dict,
                                        drupal_version_dict,
                                        ssl_dict,
                                        link_dict,
                                        tag_dict)) 

api_response = {'api_response':d}

或者,使用reduce

d = reduce(lambda x,y: dict(x, **y), (ada_dict, 
                                  wordpress_version_dict,
                                  drupal_version_dict,
                                  ssl_dict,
                                  link_dict,
                                  tag_dict))

api_response = {'api_response':d}

答案 1 :(得分:1)

如果你想在一个单一的" newDict"中添加所有的dict,如果多个Dict中存在多个键,那就是carrefull:

ada_dict={'k1':'v1'}
wordpress_version_dict={'k2':'v2'}
drupal_version_dict={'k3':'v3'}
ssl_dict={'k4':'v4'}
link_dict={'k5':'v5'}
tag_dict={'k5':'v5'}

newDict={}
newDict.update( (k,v) for k,v in ada_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in wordpress_version_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in drupal_version_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in ssl_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in link_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in tag_dict.iteritems() if v is not None)

print {'api_response':newDict}

https://repl.it/ND3p/1

答案 2 :(得分:1)

根据您的要求给出一个非常类似的例子:

>>> d1 = {'a':1}
>>> d2 = {'b':2}
>>> d3 = {'c':3}
>>> d4 = {'d':4}

#magic happens here
>>> d = {'d1':d1 , 'd2':d2, 'd3':d3, 'd4':d4}
>>> d
=> {'d1': {'a': 1}, 'd2': {'b': 2}, 'd3': {'c': 3}, 'd4': {'d': 4}}

以来,您没有想要在一个地方添加的所有词典,这非常简单。

如果您想将另一个key添加到所有词典的集合(此处为d),请执行以下操作:

>>> out = {'api_responses': d}

#or in one step if you do not want to use `d`
>>> out = {'api_responses': {'d1':d1 , 'd2':d2, 'd3':d3, 'd4':d4}}

>>> out
=> {'api_responses': {'d1': {'a': 1}, 'd2': {'b': 2}, 'd3': {'c': 3}, 'd4': {'d': 4}}}

答案 3 :(得分:0)

请为您的帖子添加更多评论,但是因为我认为您需要类似的内容,不是吗?

>>> foo=lambda dest, src, tag: [dest.update({tag:i}) for i in src]
>>> x={1:1}
>>> y={2:2}
>>> foo(x, y, "tag")
[None]
>>> x
{1: 1, 'tag': 2}
>>> y
{2: 2}

答案 4 :(得分:0)

这有助于获得所需的输出吗?

   new_dict=dict(ada_dict.items()+wordpress_version_dict.items() +drupal_version_dict.items()+ssl_dict.items()+link_dict.items()+tag_dict.items())

答案 5 :(得分:0)

 a =[ada_dict,
wordpress_version_dict,
drupal_version_dict,
ssl_dict,
link_dict,
tag_dict]

 c= {}

for i in a:
    c.update({i:i})

print c
{'ada_dict': 'ada_dict',
 'drupal_version_dict': 'drupal_version_dict',
 'link_dict': 'link_dict',
 'ssl_dict': 'ssl_dict',
 'tag_dict': 'tag_dict',
 'wordpress_version_dic': 'wordpress_version_dic'}
d={}
d.update({"api_responses":c})
print d

{'api_responses': {'ada_dict': 'ada_dict',
  'drupal_version_dict': 'drupal_version_dict',
  'link_dict': 'link_dict',
  'ssl_dict': 'ssl_dict',
  'tag_dict': 'tag_dict',
  'wordpress_version_dic': 'wordpress_version_dic'}}