Hi Experts,

I was wondering how to do the following:

I have three strings that have five numbers assigned to them for instance, 'a' can equal 1, 2, 3; 'b' can only equal 4 and 'c' can only equal 5.

some process happens and I get a result of an integer 1 - 5, say 3 - I would then like the script to tell me what string is assigned to it, in this case 'a'...

I had something like this:

z = {'a': 1, 'a': 2, 'a': 3, 'b': 4, 'c': 5)
# ~ insert process here ~
# number 3 returned
x = z.get(3)
print x 
# I expect it to tell me that 'a' is associated with 3 but I get "None"

## I also tried:
print z[2];

For which I get
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
print z;
KeyError: '2'

The key error changes from 1-5 dependent on which one I call in the print z["NUMBER"]
Any thoughts? I also tried:

z = {'a': 3, 'b': 4, 'c': 5)

but it spits this out:
x = z.get(3)
>>> print x
None


Cheers

The top line will confuse python. You should have (I think, from seeing another dictionary related post :/):

z = {'a':[1, 2, 3], 'b': 4, 'c': 5)

Not sure as I have barely used dictionaries, but I believe that one of the problems lies in line 4. You want it to tell you that 'a' is associated with 3. Are you sure it works both ways? Isn't it "3 is associated with 'a' ('a' being the 'parent')"? Is it reversable?

Also, KeyError '2', means that you are trying to search for the instance of '2' in the dictionary, which does not exist. It looks as though this lies within a module, but, taking your code as an example, though it is associated with 'a', it is not a dictionary object in itself.

You have to some reading about dictionary.
Some stuff you can look at.

>>> z = {'a': 1, 'a': 2, 'a': 3, 'b': 4, 'c': 5}
>>> z
{'a': 3, 'b': 4, 'c': 5}
>>> #Keys are unique within a dictionary while values may not be
>>> #That`s why only last value 3 is bind to key 'a'

>>> d = {'a' (1,2,3)
>>> d['a']
(1, 2, 3)
>>> #now key 'a' has value of 1,2,3
>>> d.keys()
['a']
>>> d.values()
[(1, 2, 3)]
 
>>>#some explation  about get.
>>>#dict.get(key, default=None)
>>>#For key key, returns value or default if key not in dictionary
>>> z.get('a')
3
>>> d.get('a')
(1, 2, 3)
>>> print d.get('b')
None
>>>

Edited 6 Years Ago by snippsat: n/a

Maybe this helps you to understand what happens with your code, you have the dict turned upside down, we turn it back.

>>> z = {'a': 1, 'a': 2, 'a': 3, 'b': 4, 'c': 5 }
>>> z # only last saved value for 'a' is saved
{'a': 3, 'c': 5, 'b': 4}
>>> z.keys()
['a', 'c', 'b']
>>> z.values()
[3, 5, 4]
>>> valid = { 'a': range(1,4), 'b' : [4], 'c': [5] }
>>> valid
{'a': [1, 2, 3], 'c': [5], 'b': [4]}
>>> reverse_valid = dict((a,b)
             for b in valid
             for a in valid[b])
>>> reverse_valid
{1: 'a', 2: 'a', 3: 'a', 4: 'b', 5: 'c'}
>>> reverse_valid[3]
'a'
>>>

Edited 6 Years Ago by pyTony: n/a

Not your lucky day snippsat, double post and:

>>> d = {'a' (1,2,3)}
SyntaxError: invalid syntax

P.S: inside half of hour you can edit the bug out from your post, maybe you know.

Edited 6 Years Ago by pyTony: n/a

Not your lucky day snippsat, double post and:

Yes,a misstype when a did some edting of forum code thx.
Did fix it now.

Edited 6 Years Ago by snippsat: n/a

I get a result of an integer 1 - 5, say 3 - I would then like the script to tell me what string is assigned to it, in this case 'a'...

Except that no one realized that the dictionary is bass-ackwards.

ltr_dic = {1: 'a',
           2:'a', 
           3:'a', 
           4:'b', 
           5:'c'}
# ~ insert process here ~
# number 3 returned
x = ltr_dic[3]
print x
for x in range[1, 6]:
    print x, ltr_dic[x]

Edited 6 Years Ago by woooee: n/a

Except that no one realized that the dictionary is bass-ackwards.

ltr_dic = {1: 'a',
           2:'a', 
           3:'a', 
           4:'b', 
           5:'c'}
# ~ insert process here ~
# number 3 returned
x = ltr_dic[3]
print x
for x in range[1, 6]:
    print x, ltr_dic[x]

I did answer how to put the dictionary correctly. See end of my post on reversing the dictionary correctly. Of course it is possible to put it right in first place, thought only that it is one usefull example of reversing dictionary for future.

Thanks Guys.

All of your posts helped me understand dictionaries more... Since I can give the points to only one person, I will give them to tonyjv as his explanation and code was closest to what I was trying to do myself. I appreciate all of your efforts though.

Cheers

s0ur

This question has already been answered. Start a new discussion instead.