I have a dictionary dict_matches
with 2 inner dictionaries.
The structure of dict_match
is as follows:
dict_match = {Query_ID:{Function_ID:{DB_ID:[func_ID]}}}
Within the top level of keys Query_ID
, I loop through these and compare these against keys in a completely separate dict query_count_dict
to determine the overlap of keys.
Within this loop I also navigate to the base dict in dict_matches
in order to see what keys DB_ID
the master key has 'matched' with. My problem is that this lower-level of keys DB_ID
that correspond to the very top-level key Query_ID
can be duplicated (and I only want to see the unique keys). I tried using the set() method but this actually split the string keys into their character components and printed the unique characters for each lower-level key. Any help appreciated!
See code below
for k,v in dict_match.items():
if k in query_count_dict.keys():
print(k)
detection_query.append(k)
print(len(dict_match[k])/int(query_count_dict[k]))
if type(v) is dict:
recursive_items(v)
where recursive_items is a function to navigate to the base dict:
def recursive_items(dictionary):
for k, v in dictionary.items():
if type(v) is dict:
recursive_items(v)
else:
print(set(k))
You can print all the unique keys by passing a list of keys in your recursive function, to store them as you navigate to the base. Then cast it to set to remove the duplicates.
def get_keys(dictionary, keys=[]):
for k, v in dictionary.items():
keys.append(k)
if isinstance(v, dict):
get_keys(v, keys)
return set(keys)
dict_match = {'A': {'A': {'B': [0], 'H': [0]}, 'D': {'C': [0], 'A': [0]}},
'B': {'C': {'A': [0]}, 'G': {'A': [0]}},
'C': {'A': {'E': [0]}, 'B': {'F': [0], 'A': [0]}, 'C': {'B': [0]}}}
print(get_keys(dict_match))
And it will output only the unique keys:
{'G', 'B', 'E', 'F', 'C', 'H', 'A', 'D'}
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.