User Name Password Register
DaniWeb IT Discussion Community
All
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:
Jan 19th, 2008
Views: 2,727
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.
Last edited : Jan 19th, 2008.
python Syntax | 5 stars
  1. # search a dictionary for key or value
  2. # using named functions or a class
  3. # tested with Python25 by Ene Uran 01/19/2008
  4.  
  5. def find_key(dic, val):
  6. """return the key of dictionary dic given the value"""
  7. return [k for k, v in symbol_dic.iteritems() if v == val][0]
  8.  
  9. def find_value(dic, key):
  10. """return the value of dictionary dic given the key"""
  11. return dic[key]
  12.  
  13. class Lookup(dict):
  14. """
  15. a dictionary which can lookup value by key, or keys by value
  16. """
  17. def __init__(self, items=[]):
  18. """items can be a list of pair_lists or a dictionary"""
  19. dict.__init__(self, items)
  20.  
  21. def get_key(self, value):
  22. """find the key(s) as a list given a value"""
  23. return [item[0] for item in self.items() if item[1] == value]
  24.  
  25. def get_value(self, key):
  26. """find the value given a key"""
  27. return self[key]
  28.  
  29.  
  30. # test it out
  31. if __name__ == '__main__':
  32.  
  33. # dictionary of chemical symbols
  34. symbol_dic = {
  35. 'C': 'carbon',
  36. 'H': 'hydrogen',
  37. 'N': 'nitrogen',
  38. 'Li': 'lithium',
  39. 'Be': 'beryllium',
  40. 'B': 'boron'
  41. }
  42.  
  43. print find_key(symbol_dic, 'boron') # B
  44. print find_value(symbol_dic, 'B') # boron
  45. print find_value(symbol_dic, 'H') # hydrogen
  46.  
  47. name = 'lithium'
  48. symbol = 'Li'
  49. # use a dictionary
  50. look = Lookup(symbol_dic)
  51. print look.get_key(name) # [Li']
  52. print look.get_value(symbol) # lithium
  53.  
  54. # use a list of pairs instead of a dictionary
  55. # will be converted to a dictionary by the class internally
  56. age_list = [['Fred', 23], ['Larry', 28], ['Ene', 23]]
  57. look2 = Lookup(age_list)
  58. print look2.get_key(23) # ['Ene', 'Fred']
  59. 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!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC