•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 363,779 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,461 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Most commonly a dictionary of key:value pairs is searched by the unique key. Sometimes it becomes necessary to search for the key, or keys given a value. Special consideration has to be given to this case, because the value does not have to be unique and may return several keys (list of keys).
A class inheriting the dictionary base class is used to do both searches. I have also made up some simple, but nicely named functions to search the dictionary, in case you don't want to use a class.
A class inheriting the dictionary base class is used to do both searches. I have also made up some simple, but nicely named functions to search the dictionary, in case you don't want to use a class.
Last edited : Jan 19th, 2008.
# search a dictionary for key or value # using named functions or a class # tested with Python25 by Ene Uran 01/19/2008 def find_key(dic, val): """return the key of dictionary dic given the value""" return [k for k, v in symbol_dic.iteritems() if v == val][0] def find_value(dic, key): """return the value of dictionary dic given the key""" return dic[key] class Lookup(dict): """ a dictionary which can lookup value by key, or keys by value """ def __init__(self, items=[]): """items can be a list of pair_lists or a dictionary""" dict.__init__(self, items) def get_key(self, value): """find the key(s) as a list given a value""" return [item[0] for item in self.items() if item[1] == value] def get_value(self, key): """find the value given a key""" return self[key] # test it out if __name__ == '__main__': # dictionary of chemical symbols symbol_dic = { 'C': 'carbon', 'H': 'hydrogen', 'N': 'nitrogen', 'Li': 'lithium', 'Be': 'beryllium', 'B': 'boron' } print find_key(symbol_dic, 'boron') # B print find_value(symbol_dic, 'B') # boron print find_value(symbol_dic, 'H') # hydrogen name = 'lithium' symbol = 'Li' # use a dictionary look = Lookup(symbol_dic) print look.get_key(name) # [Li'] print look.get_value(symbol) # lithium # use a list of pairs instead of a dictionary # will be converted to a dictionary by the class internally age_list = [['Fred', 23], ['Larry', 28], ['Ene', 23]] look2 = Lookup(age_list) print look2.get_key(23) # ['Ene', 'Fred'] print look2.get_value('Fred') # 23
Comments (Newest First)
teci | Newbie Poster | 29 Days Ago
•
•
•
•
Much thanks! Your code is very helpful (and efficient too!) 
However, in defining "find_key", shouldn't you use the argument "dic" instead of "symbol_dic" in calling iteritems?
Other than that, thanks for the big help!

However, in defining "find_key", shouldn't you use the argument "dic" instead of "symbol_dic" in calling iteritems?

Other than that, thanks for the big help!
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)