1

I have a nested dictionary which is very similar to the one described in this link.

User arainchi posted the following function there:

def findkeys(node, kv):
    if isinstance(node, list):
        for i in node:
            for x in findkeys(i, kv):
               yield x
    elif isinstance(node, dict):
        if kv in node:
            yield node[kv]
        for j in node.values():
            for x in findkeys(j, kv):
                yield x

If I do

print (list(findkeys(d, 'id')))

the children of the given key as a generator object type are printed.

When I tried, listing down keys() by

print ((findkeys(d, 'id')).keys())

I am getting

AttributeError: 'generator' object has no attribute 'keys'

How can I get the keys of the retrieved children?

Example:

cfg_dict = { 'mobile' :
                { 'checkBox_OS' :
                  { 'status' : 'None', 
                    'radioButton_Andriod' :
                      { 'status' : 'None',
                        'comboBox_Andriod_Brands' : 'LG'},
                    'radioButton_Windows' :
                      { 'status' : 'None',
                        'comboBox_Windows_Brands' : 'Nokia'},
                    'radioButton_Others' :
                      { 'status' : 'None',
                        'comboBox_Others_Brands' : 'Apple'}},
                  'checkBox_Screen_size' :
                    { 'status' : 'None',
                      'doubleSpinBox_Screen_size' : '5.0' }}
              }

print ("findkeys: ", findkeys(self.cfg_dict, "radioButton_Andriod"))
print ("list of findkeys:", list(findkeys(self.cfg_dict, "radioButton_Andriod")))
print ("keys of findKeys:", list(findkeys(self.cfg_dict, "radioButton_Andriod"))[0].keys())

Output:

findkeys:  <generator object findkeys at 0x02F0C850>
list of findkeys: [{'status': False, 'comboBox_Andriod_Brands': 'Sony'}]
keys of findKeys: dict_keys(['status', 'comboBox_Andriod_Brands'])

I want to iterate over the keys of the child. something like,

#pseudo code        
for everykey in child.keys()
  if everykey.value == "this":
    #do this
  else:
    #do that
Community
  • 1
  • 1
sreeKang
  • 35
  • 2
  • 8

2 Answers2

3
list(findkeys(d, 'id'))[0].keys()

for keys of the first child found. It will bug if it s not a dict so you will probably need to check

edit : for what you ask in your new edit

for value in findkeys(d, 'id'):
    for child_key, child_value in value.items():
        if child_value == 'this':
            # do this
        else:
            # do that
Ludovic Viaud
  • 202
  • 1
  • 5
  • Solution looks like `for everykey in list(findkeys(self.cfg_dict, 'comboBox_Others_Brands'))[0].keys(): kvalue = list(findkeys(self.cfg_dict, 'comboBox_Others_Brands'))[0].get(everykey)` – sreeKang Nov 17 '14 at 14:59
  • if cfg_dict is an object attribute and findkeys a method of this object, why pass cfg_dict as argument ? perhaps it would be better to give the format of your data, what you want to do, and what you have for now, it would be more clear – Ludovic Viaud Nov 17 '14 at 16:55
0

I've made a pypi package for this. You can refer here https://test.pypi.org/project/dictnodefinder/1.0.1.dev1/

Install it first

pip install dictnodefinder

Then use it like this

import dictnodefinder

source = {‘a’:{‘b’:0}}    
key_to_find = ‘b’

print(dictnodefinder.findkeys(source, key_to_find))
Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108