2

I have a very big dictionary of dictionaries and it is like this:

Dict={,
    ...
    '25465466':{'Cmstrk': 'cms_trk_dcs_05:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard06', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},
    '436232302': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate1', 'Board': 'easyBoard01', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_8', 'Channel': 'channel002\n'},
    '470311412': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard00', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},
    ...
} 

And if the user types cms_trk_dcs_05:CAEN or easyCrate1/easyBoard01 or more combination of this values the script has to return those keys (numbers like '654546536') that they have in common.

For example if input is easyCrate0/CMS_TRACKER_SY1527_4 the answer is 470311412,25465466.

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
karla
  • 23
  • 3
  • When do you use the keys within the inner dictionary? Currently with the way you have it set up, you can only access the inner dictionary (with keys such as "Cmstrk" and "Crate") with keys such as "25465466". Are you wanting to have the user input something like "cms_trk_dcs_05:CAEN" then return a number such as "25465466" or vice versa? – Dylan Jul 24 '14 at 14:09
  • Relevant: http://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary – wnnmaw Jul 24 '14 at 14:09
  • yes i want to know the keys in common to some values – karla Jul 24 '14 at 14:14

3 Answers3

0

I guess this should help.

Dict = {
'25465466':{'Cmstrk': 'cms_trk_dcs_05:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard06', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'},

'436232302': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate1', 'Board': 'easyBoard01', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_8', 'Channel': 'channel002\n'},

'470311412': {'Cmstrk': 'cms_trk_dcs_03:CAEN', 'Crate': 'easyCrate0', 'Board': 'easyBoard00', 'Branch': 'branchController05', 'TrackerSY': 'CMS_TRACKER_SY1527_4', 'Channel': 'channel003\n'}
}

def sub_search(terms):
    check = lambda k: all(map(lambda term:term in Dict[k].values(), terms))
    return [key for key,value in Dict.items() if check(key)]

def search(term):
    terms = term.split('/')
    return sub_search(terms)

search('cms_trk_dcs_05:CAEN')
#['25465466']

search('easyCrate1/easyBoard01')
#['436232302']
shshank
  • 2,571
  • 1
  • 18
  • 27
0
inp = "easyCrate0/CMS_TRACKER_SY1527_4".split("/")
common = []
for k in Dict:
    if all(x in Dict[k].values() for x in inp): # if all of the values match the user input, add it to common list
        common.append(k) 
print (common)
['25465466', '470311412']

You could also use sets:

common = []
for k in Dict:
    if len(set(inp).intersection(Dict[k].values())) == len(inp):
        common.append(k)

You can use a list comp also:

common = [k  for k in Dict if len(set(inp).intersection(Dict[k].values())) == len(inp)]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

FINAL EDIT: Here is the code cleaner:

from collections import OrderedDict

find = raw_input("What are you looking for? ")
finder = find.split('/')

similar = []
for key in Dict:
    for x in range(0, len(finder)):
        for pairs in Dict[key].items():
            if pairs[1] == finder[0]: 
                similar.append(key)

if len(similar) == 1 or len(finder) == 1:
    print ",".join(similar) 
for items in similar:
    similar.remove(items)
print ",".join(list(OrderedDict.fromkeys(similar)))
Mburdzy
  • 35
  • 4