simple question about dictionaries
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Jul 22 11:38:35 EDT 2008
skazhy a écrit :
> hi, i am new to python, so i've a really simple question about
> dictionaries.
> if i have a dictionary and I make have an input after it (to input
> numbers) can i get the key of value that was in input?
What if many keys are associated with a same value, ie:
d = {'a':100, 'b':200, 'c':100}
> somehting like this:
> dict = { "key1"=100,"key2"=200,"key3"=300}
1/ don't use 'dict' as an indentifier - this shadows the builtin dict type.
2/ the 'dict literal' syntax is:
d = {'key1":100, "key2": 200, "key3":300}
> a = input()
> print 'the key of inputted value is', dict['a']
>
> this syntax of course is wrong, but i hope you got the point..
Assuming you don't have duplicate values, you just need to build a
reverse dictionnary, that is using values as keys and keys as values,
then lookup this dictionnary. The first operation can be done in one
single line.
reversed_d = dict((value, key) for key, value in d.items())
More information about the Python-list
mailing list