Dictionaries inside out

Terry Reedy tjreedy at udel.edu
Fri Nov 26 16:43:28 EST 2010


On 11/26/2010 1:13 PM, Greg Lindstrom wrote:
> I am working on a project where I'm using dictionaries to hold the
> translations to codes (i.e., {'1':'Cheddar','2':'Ice
> Hockey','IL':'Thermostat Broken'}).  The bulk of the application
> requires me to translate codes to their meaning, but it would be nice to
> be able to translate a meaning back to the original code as well.  This
> seems to me like it must be a common situation that has been
> addresses/solved by those smarter than me.  Is there, dare I say, a
> design pattern for this problem?  Is there a better way of approaching
> it other than making a set of dictionaries which "mirror" the
> originals?  FWIW, I have approximately 50 tables ranging from 2 entries
> to over 100.

Making 50 inverse dictionaries is trivial. Depending on your need, you 
might instead consider one inverse dict that maps 'meaning' to 
(table,code) pairs (assuming that meanings are not unique across the 50 
original tables.

-- 
Terry Jan Reedy




More information about the Python-list mailing list