简体   繁体   English

Python在dict_keys和交换值中查找dict和嵌套值

[英]Python lookup dict and nested values in dict_keys and swap value

I'm attempting to swap values in dictionary(dic_map) with values from dictionary(B), where dic_map[key] = B. to give me a new dict with the values from B eg我正在尝试将字典(dic_map)中的值与字典(B)中的值交换,其中 dic_map[key] = B. 给我一个新的字典,其中包含来自 B 的值,例如

dic_map = {
    'Name': 'name',
    'Id': 'guid',
    'address':{
        'address1': 'road',
        'address2': 'road_nr',
        'address3': 'zipcode',
        'address4': 'city'
    },
    'tax': True,
    'isValid': False,
    'specific': 1,
    'credit': 200
}


B = {
    'name': 'Michael Jackson',
    'guid': '032-567-781',
    'road': 'The greatest Dr',
    'road_nr': 42,
    'zipcode': 90210,
    'city': 'Hollywood',
    'country': 'USA'
}

And the final dict和最后的字典

desired_result = {
    'Name': 'Michael Jackson',
    'Id': '032-567-781',
    'address':{
        'address1': 'The greatest Dr',
        'address2': 42,
        'address3': 90210,
        'address4': 'Hollywood',
    },
    'tax': True,
    'isValid': False,
    'specific': 1,
    'credit': 200
}

This is what I have so far这是我到目前为止

def swap_values(dic_values, dic_map):
    d = {}
    for k1 in dic_values.keys():
        for k2 in dic_map.keys():
            if k1 == dic_map[k2]:
                d[k2]=dic_values[k1]
            else:
                d[k2]=dic_map[k2]
    return d

There are 2 things that is not working as far as I can tell, 1. my values gets overwritten by the else statement, and 2. the nested dict is never evaluated.据我所知,有两件事不起作用,1. 我的值被 else 语句覆盖,2. 嵌套的 dict 永远不会被评估。 Any help would be appreciated.任何帮助,将不胜感激。

As there are inner dicts, we must repeat the replacement operation for every inner dict.由于有内部字典,我们必须对每个内部字典重复替换操作。 The easiest way to perform that kind of task is using recursion.执行此类任务的最简单方法是使用递归。

Validating type is not considered very pythonic , but it will get the job done.验证类型不被认为是非常pythonic ,但它会完成工作。 You can do something like this:你可以这样做:

def swap_values(dic_values, dic_map):
    d = {}

    for k, v in dic_values.items():
        if isinstance(v, dict):
            d[k] = swap_values(v, dic_map)
        else:
            d[k] = dic_map.get(v, v)

    return d

Sample usage:示例用法:

>>> dic_map = {
...     'Name': 'name',
...     'Id': 'guid',
...     'address':{
...         'address1': 'road',
...         'address2': 'road_nr',
...         'address3': 'zipcode',
...         'address4': 'city'
...     },
...     'tax': True,
...     'isValid': False,
...     'specific': 1,
...     'credit': 200
... }
>>> 
>>> 
>>> B = {
...     'name': 'Michael Jackson',
...     'guid': '032-567-781',
...     'road': 'The greatest Dr',
...     'road_nr': 42,
...     'zipcode': 90210,
...     'city': 'Hollywood',
...     'country': 'USA'
... }
>>> swap_values(dic_map, B)
{'specific': 1, 'Name': 'Michael Jackson', 'credit': 200, 'address': {'address1': 'The greatest Dr', 'address2': 42, 'address4': 'Hollywood', 'address3': 90210}, 'isValid': False, 'tax': True, 'Id': '032-567-781'}

If there were no inner dicts, we could use:如果没有内部字典,我们可以使用:

def swap_values(dic_values, dic_map):
    d = {}

    for k, v in dic_values.items():
        d[k] = dic_map.get(v, v)

    return d

Sample usage:示例用法:

>>> dic_map = {
...     'Name': 'name',
...     'Id': 'guid',
...     'tax': True,
...     'isValid': False,
...     'specific': 1,
...     'credit': 200
... }
>>> 
>>> 
>>> B = {
...     'name': 'Michael Jackson',
...     'guid': '032-567-781',
...     'road': 'The greatest Dr',
...     'road_nr': 42,
...     'zipcode': 90210,
...     'city': 'Hollywood',
...     'country': 'USA'
... }
>>> swap_values(dic_map, B)
{'specific': 1, 'isValid': False, 'Name': 'Michael Jackson', 'Id': '032-567-781', 'tax': True, 'credit': 200}

Here's what I got:这是我得到的:

def dmap(template, values):
    for key, val in template.items():
        if isinstance(val, dict):
            dmap(val, values)
        else:
            if val in values:
                template[key] = values[val]

If I run dmap(dic_map, B) I get:如果我运行dmap(dic_map, B)我得到:

{
  'Id': '032-567-781',
  'Name': 'Michael Jackson',
  'address': {
    'address1': 'The greatest Dr',
    'address2': 42,
    'address3': 90210,
    'address4': 'Hollywood'
  },
  'credit': 200,
  'isValid': False,
  'specific': 1,
  'tax': True
}

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

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