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