简体   繁体   English

替换字典或嵌套字典或字典列表中键值的泛型函数

[英]Generic Function to replace value of a key in a dict or nested dict or list of dicts

So i have a Dict like so: 所以我有一个像这样的词典:

{
  "environment": [
    {
      "appliances": [
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        },
        {
          "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        }
      ],
      "vd_url": "https://bla1"
    },
    {
      "appliances": [
        {
          "ipAddress": "10.4.64.108"
          "type": "branch",
          "sync-status": "IN_SYNC"
        },
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
          "sync-status": "IN_SYNC"
        }
      ],
      "vd_url": "https://bla2"
    },
  ],
  "failed_urls": [
    "https://gslburl",
    "https://gslburl",
    "https://localhost",
    "https://localhost",
    "https://localhost"
  ]
}

or it can be something like this 或可能是这样的

{
  "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
  "services-status": "GOOD",
  "ping-status": "REACHABLE"
  "vd_url" : "https://blah3"
}

or it can be something like this 或可能是这样的

{
  "environment": [
    {
      "appliances": [
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        },
        {
          "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        }
      ],
      "vd_url": "https://bla1"
    }
  ]
}

or it can be in any combination of dictionaries possible, but what i am trying to achieve is write a generic function that can take the dictionary, replace a key with another value and return the new dict. 或者它可以是字典的任意组合,但是我试图实现的是编写一个通用函数,该函数可以接收字典,用另一个值替换键并返回新字典。 Just a pseudo code: 只是一个伪代码:

def replace(dict_obj, key, new_value)
    for dict in dict_obj:
        for k, v in dict.items()
        if k == key:
              dict[k] = new_value

    return dict_obj

but in the end i want the same dictionary object - dict_obj that i passed but with the new values and it should work for any type of dicts above Scratching my head as to how to solve it :( 但是最后我想要一个与我传递的字典相同的字典对象-dict_obj,但是具有新的值,它应该适用于上面的任何类型的字典。

You can use recursion to determine if value from a key,value pair is a list or a dict and then iterate accordingly. 您可以使用递归来确定键,值对中的值是列表还是字典,然后进行相应的迭代。

def replace(dict_obj, key, new_value):
    for k,v in dict_obj.iteritems():
        if isinstance(v,dict):
            # replace key in dict
            replace(v, key, new_value)
        if isinstance(v,list):
            # iterate through list
            for item in v:
                if isinstance(item,dict):
                    replace(item, key, new_value)
        if k == key:
            dict_obj[k] = new_value

暂无
暂无

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

相关问题 替换嵌套列表和字典的字典中的项目 - Replace items in a dict of nested list and dicts 带有嵌套字典的列表中的数据帧,其中第一个字典的键是列和键,第二个字典的值是行和值 - DataFrame from list with nested dicts where key of first dict is column and key, value of second dict is row and value 将嵌套字典的键替换为另一个字典的值(两个字典的键相等)),键的值可以是dicts列表 - Replace keys of a nested dictionary with the value of another dictionary(where keys of two dict are equal)), value for a key may be list of dicts 如何将字典/嵌套字典的字典转换为列表字典 - How do I convert dict of dicts/nested dicts into dict of list 如何从具有嵌套字典列表的字典中提取特定键值对 - how to extract a specific key value pair from a dict with a nested list of dicts 如何检查字典是否在字典的键和值列表中,字典可以嵌套在字典的列表中? - How to check if dictionary is within a list of dictionaries on both key and value of dict, where dicts can be nested? 如果dict键的值在otherlist中,则从列表中的dicts中删除元素 - Delete elements from dicts in list if value of dict key is in otherlist 按键将字典分组后,获取字典列表中具有最大值的项目 - Getting the item with the maximum value in list of dicts after grouping the dict by key 如何在一个dicts列表中获得具有公共密钥最大值的整个dict - How to get whole dict with max value of a common key in a list of dicts 在dicts列表中,根据一个键/值匹配dict? - In list of dicts, match dict based on one key/value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM