如何合并两个字典列表

时间:2015-12-09 06:53:04

标签: python dictionary

我的第一个清单是

"networks":[
{"name": "public", "ip_ranges":[["172.16.0.2", "172.16.0.126" ] ], "id": 173,…},
{"name": "management", "ip_ranges":[["192.168.0.1", "192.168.0.254" ] ],…},
{"name": "storage", "ip_ranges":[["192.168.1.1", "192.168.1.254" ] ],…},
{"name": "private", "ip_ranges":[["192.168.2.1", "192.168.2.254" ] ],…},
{"name": "public", "ip_ranges":[["172.16.0.2", "172.16.0.126" ] ],…},
{"name": "management", "ip_ranges":[["192.168.0.1", "192.168.0.254" ] ],…},
{"name": "storage", "ip_ranges":[["192.168.1.1", "192.168.1.254" ] ],…},
{"name": "private", "ip_ranges":[["192.168.2.1", "192.168.2.254" ] ],…},
{"name": "fuelweb_admin", "ip_ranges":[], "id": 181, "meta":{"notation": "ip_ranges",…},
{"name": "fuelweb_admin", "ip_ranges":[["10.30.0.3", "10.30.0.254" ] ], "id": 1,…}
]

和第二个dict是这样的

 "network_conf": [

            {
                "network_name": "default",
                "public": {
                    "ip_ranges": {
                        "start": "135.21.67.150",
                        "end": "135.21.67.170"
                    },
                    "cidr": "135.21.67.128/26",
                    "gateway": "135.21.67.129",
                    "floating_ip_ranges": {
                        "start": "135.21.67.171",
                        "end": "135.21.67.180"
                    }

                },
                "storage": {
                    "ip_ranges": {
                        "start": "192.168.109.100",
                        "end": "192.168.109.200"
                    },
                    "cidr": "192.168.109.0/24"

                },
                "management": {
                    "ip_ranges": {
                        "start": "192.168.27.150",
                        "end": "192.168.27.190"
                    },
                    "cidr": "192.168.27.128/26",
                    "gateway": "192.168.27.129"

                }
            },

            {
                "network_name": "ops_test_network_0",
                "public": {
                    "ip_ranges": {
                        "start": "135.21.67.160",
                        "end": "135.21.67.170"
                    },
                    "cidr": "135.21.67.128/26",
                    "gateway": "135.21.67.129",
                    "floating_ip_ranges": {
                        "start": "135.21.67.171",
                        "end": "135.21.67.180"
                    }

                },
                "storage": {
                    "ip_ranges": {
                        "start": "192.168.109.100",
                        "end": "192.168.109.200"
                    },
                    "cidr": "192.168.109.0/24"

                },
                "management": {
                    "ip_ranges": {
                        "start": "192.168.27.150",
                        "end": "192.168.27.190"
                    },
                    "cidr": "192.168.27.128/26",
                    "gateway": "192.168.9.1"

                }
            }

    ],

我想将dict['name']==public的1个列表合并到第二个dict键“public”

更好的方法是什么, 如果有更简单的方法可以相应地修改第二个字典 主要问题是按键public => publicmanagement=>management等等映射到不同的词典列表

1 个答案:

答案 0 :(得分:0)

我有两个相同长度的列表:

[1,2,3,4][a,b,c,d]

dict(zip([1,2,3,4], [a,b,c,d]))

如果列表很大,您应该使用itertools.izip

如果键数多于值,并且您想要填写额外键的值,则可以使用itertools.izip_longest

这里,a,b,c和d是变量 - 它可以正常工作(只要它们被定义),但如果你想要它们作为字符串你可能意味着['a','b','c','d']

zip从每个iterable获取第一个项目并生成一个元组,然后从每个迭代中获取第二个项目等等。

dict可以迭代迭代,其中每个内部iterable都有两个项 - 然后使用第一个作为键,第二个作为每个项的值。