You are not logged in.
Hello fellow archlinux users and python programming overlords,
I have a problem with getting my code to work. I want to be able to search for a key inside a dictionary using a value.
For example assuming that we have
test1= {'speed': 'm/s', 'length': 'm'}
I want to be able to search for 'm/s' and get 'speed'.
I have managed to do that using the following piece of code.
def find_key(dic, val):
return [k for k, v in dic.items() if v == val][0]
However when I try to use the function with a dictionary that includes a list for each key, for example:
test2 = {'speed': ['m/s', 'km/h'], 'length': ['m', 'in']}
I get the following error:
Traceback (most recent call last):
File "find_key.py", line 8, in <module>
key = find_key(test2, 'm')
File "find_key.py", line 6, in find_key
return [k for k, v in dic.items() if v == val][0]
IndexError: list index out of range
Any help will be appreciated, and thank you in advance.
Last edited by baion baion (2010-07-17 22:50:48)
Offline
Maybe you should switch and look for key, not value?
test1= {'m/s': 'speed', 'm': 'length'}
As for test2: http://docs.python.org/library/stdtypes … types-dict
A dictionary's keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys.
Last edited by karol (2010-07-17 18:47:36)
Offline
[k for k, v in dic.items() if v == val][0]
You are creating a list and referencing the first item in the list.
IndexError: list index out of range
You are getting this error because there were no matches so nothing was put into the list. You can't reference the first item on an empty list because there is no first item.
The reason your code doesn't 'm' in the second example is because the value returned for each item in the dictionary is a list. meaning the value for key 'length' is ['m', 'in']. 'm' will not equal ['m', 'in']. For this to work you will need to check each item in the list of values and see if one of them matches.
Offline
I'm not a dev, I don't know python, but you should structure your data wrt how it will be accessed right?
Another thing - speed is measured in km/h, not km.
Last edited by karol (2010-07-17 19:18:42)
Offline
I'm not a dev, I don't know python, but you should structure your data wrt how it will be accessed right?
Another thing - speed is measured in km/h, not km.
I corrected the km to km/h, boy I guess I need some sleep.
@user_none:
So you are saying that instead of using
if v == val
I should use
if val in v
Right?
Well I will try it and post back. In the meantime if you think that what I wrote makes no sense let me know.
Last edited by baion baion (2010-07-17 21:24:52)
Offline
Right?
That will work. However, it will only work if your values are a list. If you have lists in your list it won't work. Also, it assumes each key will have unique values and will exist.
Like karol mentioned, you are using the concept of a dictionary backwards. You should be checking for the key and getting it's value not checking for the value and getting the key. You have have multiple keys that have the same value. So:
>>> l = {'m/s': 'speed', 'm': 'length', 'km/h': 'speed', 'in': 'length'}
>>> print l.get('m', None)
length
>>> print l.get('x', None)
None
Offline
You are right, that makes more sense.
Thank you karol and user_none.
Last edited by baion baion (2010-07-17 22:50:19)
Offline