(Mar-08-2020, 01:31 PM)ibreeden Wrote: Hello Mart79,
No, that is not what I mean. I meant this:
from collections import OrderedDict # the data appears to contain an OrderedDict. def recurse_object(obj_to_inspect, val_to_find, indexpath=""): if isinstance(obj_to_inspect, dict): for key, value in obj_to_inspect.items(): recurse_object(value, val_to_find, indexpath + f"['{key}']") if isinstance(obj_to_inspect, list): for key, value in enumerate(obj_to_inspect): recurse_object(value, val_to_find, indexpath + f"[{key}]") if isinstance(obj_to_inspect, str): if obj_to_inspect == val_to_find: print(f"Value {val_to_find} found at {indexpath}") dictionary = {'nestedA': OrderedDict([('Name', 'TestCase'), ('VarA', 'Local'), ('VarB', None), ('VarC', None), ('VarD', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), ('VarE', 'ABCD'), ('VarF', 10.0), ('VarG', False)]), 'nestedB': OrderedDict([('LocA', {'v1': 'ABCD', 'v2': None, 'v3': None, 'v4': [[[0, 1], [1, 1], [2, 2], [0, 4]], [[0, 2], [1, 2], [2, 3], [4, 4]], ['name', 'ABCD']]}), ('LocB', None), ('LocC', None), ('LocD', True), ('LocE', True), ('LocF', False), ('LocG', False)])} recurse_object(dictionary, 'ABCD')By the way: You used
Output:Value ABCD found at ['nestedA']['VarE'] Value ABCD found at ['nestedB']['LocA']['v1'] Value ABCD found at ['nestedB']['LocA']['v4'][2][1]dict = {'nestedA': ... etc
. Never use reserved words (like dict) as a name of a variable.
@ ibreeden, thank you so much! Yes, I did not realized it at first that I was using a reserved word.
Unfortunately, I have a second problem. The dictionary contains object information from other objects which do not get searched through.
I can't explain it other than an example:
dictionary = <class 'dict'>: {'case': 'Test',......,'checker': <checking.check.assessment object at 0x0003432C2D5646EB8>the checking.check.assessment contains object information, dictionaries, lists etc. too.
How do I search through these objects for the same 'ABCD'?