简体   繁体   English

在python中添加了多个字典

[英]addition of more than one dict in python

i have 6 dict like this 我有6个字典,像这样

dict1
dict2
dict3
dict4
dict5
dict6

now I want all of this in one dict. 现在我希望所有这些都在一个字典中。 so I used this 所以我用这个

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

at last dict6 contains all value but it's not formatted correctly and it's not the pythonic way to do it 最后dict6包含所有值,但格式不正确,也不执行该操作的pythonic方法

I want to improve this any suggestions 我想改善这个任何建议

right now I'm getting like this but I don't want like this 现在我变得像这样,但我不想这样

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

i want 我想要

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

Try this: 尝试这个:

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}

Or this, using reduce : 或者这样,使用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}

If you want add all dict in a single one "newDict", Be carrefull if several keys exist in multiple Dict : 如果要将所有字典添加到一个“ newDict”中,如果多个Dict中存在多个键,请确保完整:

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 https://repl.it/ND3p/1

Giving a very similar example of my own based on your requirements: 根据您的要求举一个非常相似的例子:

>>> 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}}

Since you do not have all the dictionaries that you want added in one place, this is about as easy as it gets. 由于您没有将要添加的所有字典都放在一个位置,因此这变得非常容易。

In case you want to add another key to your collection of all dictionaries ( d here), do: 如果要向所有词典集合中添加另一个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}}}

please add more comment to your post, but as I see you need something like , is not it ? 请在您的信息中添加更多评论,但据我所知,您需要类似的东西,不是吗?

>>> 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}

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

   new_dict=dict(ada_dict.items()+wordpress_version_dict.items() +drupal_version_dict.items()+ssl_dict.items()+link_dict.items()+tag_dict.items())
 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'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM