0

I have a txt file with a lot of lines like this

369136986 cms_trk_dcs_05:CAEN/CMS_TRACKER_SY1527_8/branchController05/easyCrate1/easyBoard03/channel002

Two columns where the first has numbers, and the second column a line with properties separated by /, both columns are separated by space.

I did a dictionary of dictionaries where the key1 is
the first number that appears in the line (369136986), this key has as a value the other dictionary, where the keys are cmstrk, trackersy, branch, crate, board, channel and each of these keys key has as a value cms_trk_dcs_05:CAEN, CMS_TRACKER_SY1527_8, branchController05, easyCrate1, easyBoard03, channel002, respectively, so if you ask cmstrk (key2) for 369136986 (key1) it returns cms_trk_dcs_05 (value).

How can I get the key by giving the value? I mean if I give the value CMS_TRACKER_SY1527_8 I need to know to which key1 corresponds (the program should return 369136986).

This is what i tried:

input3=raw_input("Write the property(s) which modules connected you want to know, separated by a single space \n > ")
input_list3=input3.split(' ')
for k in input_list3:
    print "%r" % k
    txt.write("\t\n The modules with property %r are:\n" % k)
    for l,m in zip(HVInfoDict.keys(),HVInfoDict.values()):
        if k == HVInfoDict[l][m]:
            print l

but it returns

TypeError: unhashable type: 'dict'

so how can I get the first key?

5
  • 1
    Does changing the if statement to if k in m.values(): get you what you want? Commented Jul 22, 2014 at 15:42
  • Could you put an example of the input dictionary that's a little easier to cut-and-paste? The problem here is that m is sometimes a dictionary, which can't be used as a key to another dictionary. Commented Jul 22, 2014 at 15:43
  • Also you can replace zip(HVInfoDict.keys(),HVInfoDict.values()) by HVInfoDict.items(), as it will return same list of tuples. Commented Jul 22, 2014 at 16:18
  • David Robinson, yes i had to look in m.values, thank you! Commented Jul 23, 2014 at 9:49
  • jonrsharpe, now it is solved thanks to David Robinson and Jhon Rudell, thanks for your comment. pavel_form thanks to you too Commented Jul 23, 2014 at 9:50

1 Answer 1

1

m is the whole dictionary thats retured in HVInfoDict[l].. you need to look at the values inside of m like so.

HVInfoDict = {
    369136986: {
        'cmstrk': 'cms_trk_dcs_05:CAEN',
        'trackersy': 'CMS_TRACKER_SY1527_8',
        'branch': 'branchController05',
        'crate': 'easyCrate1',
        'board': 'easyBoard03',
        'channel': 'channel002'
    }
}

input3 = raw_input("Write the property(s) which modules connected you want to know, separated by a single space \n > ")
input_list3 = input3.split(' ')
for k in input_list3:
    print "%r" % k
    txt.write("\t\n The modules with property %r are:\n" % k)
    for l,m in HVInfoDict.items():
        if k in m.values():
            print l
        else:
            print("Does not exist!")

output:

Write the property(s) which modules connected you want to know, separated by a single space 
 > branchController05 CMS_TRACKER_SY1527_8 channel002 abcdefg
'branchController05'
369136986
'CMS_TRACKER_SY1527_8'
369136986
'channel002'
369136986
'abcdefg'
Does not exist!
Sign up to request clarification or add additional context in comments.

1 Comment

Jhon Rudell you are correct i was looking in the wrong place, thanks a lot for your ansewer, my script now works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.