简体   繁体   中英

find all dictionary matching key=value pairs in dynamic dictionary

If I have a python dictionary like the following:

conf = {
         'memory': {
           'alarm': {
             'active': 'yes',
             'pagefile_error': {
               'active':'no'
             }
           }
         },
         'disk': {
           'alarm':{
             'active':'yes',
             'fixed':{
               '#dev':{
                 'active':'yes',
                 'something':'else'
               }
             }
           }
         },
         'cpu': {
           'alarm': {
             'active':'no',
             'highcpu': {
               'active':'yes'
             }
           }
         }
       }

how can I filter only the paths that end in 'active':'yes' and not show any other info.

In addition, for parent items that show up as active: no, I would want to disregard whatever comes after those.

conf = {
         'memory': {
           'alarm': {
             'active': 'yes'
           }
         },
         'disk' : {
           'alarm':{
             'active':'yes',
             'fixed': {
               '#dev': {
                 'active':'yes'
                }
              }
            }
          }
        }

I don't have any working code for this yet as I'm not sure where to start. all I have at the moment is the starting dictionary.

You can use recursion:

def active(d):
  _r, _flag = [], False
  for a, b in d.items():
    if a == 'active' and not _flag:
       _r.append(b == 'yes')
       _flag = True
    if not _flag and isinstance(b, dict):
       _r.append(active(b))
  return all(_r)

def build(d, flag = False):
  return {a:b if not isinstance(b, dict) else build(b, 'active' in b) 
    for a, b in d.items() if ((not isinstance(b, dict) and not flag) or a == 'active') or (isinstance(b, dict) and active(b))}

import json
print(json.dumps(build(conf), indent=4))

Output:

{
  "memory": {
    "alarm": {
        "active": "yes"
    }
},
 "disk": {
    "alarm": {
        "active": "yes",
        "fixed": {
            "#dev": {
                "active": "yes"
            }
        }
     }
   }
}

Using recursion :

def keep_active_only(my_dict):
    result_dict = {}

    for key, value in my_dict.items():

        # If there is embedded dict
        if isinstance(value, dict):
            # Compute the embedded dict using recursion
            result_subdict = keep_active_only(value)

            # Keeping result only if not empty
            if result_subdict:
                result_dict[key] = result_subdict

        # Keep active key if value is yes
        elif key == "active" and value == "yes":
            result_dict[key] = value

        # Returns empty dict if active is no
        elif key == "active" and value == "no":
            return {}

    return result_dict

Output :

>>> keep_active_only(conf)
{
  'disk': {
    'alarm': {
      'active': 'yes',
      'fixed': {
        '#dev': {
          'active': 'yes'
        }
      }
    }
  },
  'memory': {
    'alarm': {
      'active': 'yes'
    }
  }
}

Not sure if I understand correctly, but here is a function that discards all data from the dict that does not take you to a particular key and value:

def filter_dict(d, key, value):
    new_dict = {}
    for d_key, d_value in d.items():
        if d_key == key and d_value == value:
            new_dict[d_key] = d_value
        elif isinstance(d_value, dict):
            child = filter_dict(d_value, key, value)
            if child:
                new_dict[d_key] = child
    return new_dict

Here is how you would use it in your example:

from pprint import pprint

conf = {
    'memory': {
        'alarm': {
            'active': 'yes',
            'pagefile_error': {
                'active':'no'
            }
        }
    },
    'disk': {
        'alarm': {
            'active': 'yes',
            'fixed': {
                '#dev': {
                    'active': 'yes',
                    'something': 'else'
                }
            }
        }
    }
}
pprint(filter_dict(conf, 'active', 'yes'))
# {'disk': {'alarm': {'active': 'yes', 'fixed': {'#dev': {'active': 'yes'}}}},
#  'memory': {'alarm': {'active': 'yes'}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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