Help with a reverse dictionary lookup

Lonnie Princehouse finite.automaton at gmail.com
Thu Mar 9 21:50:33 EST 2006


The parsing is good; the structure can be transformed after the parsing
is done.

The basic problem with the "reverse lookup" search is that you need to
iterate over lots of things.  If you're only doing one search, that's
not too horrible  But if you're going to perform multiple searches, you
can do the iteration once and cache the results.  This is a trade-off
between space and time, but the space shouldn't be a problem on a
modern computer unless you've got millions of nodes.

# take the already-defined dict and make some lookup tables with
foundries and processes as keys
from sets import Set
foundry_lookup_table = {}
process_lookup_table = {}

for node, foundries in dict.iteritems():
  for foundry, processes in foundries.iteritems():
    if foundry not in foundry_lookup_table:
      foundry_lookup_table[foundry] = Set([node])
    else:
      foundry_lookup_table[foundry].add(node)
    for process in processes:
      if node not in process_lookup_table:
        process_lookup_table[process] = Set([node])
      else:
        process_lookup_table[process].add(node)

# Now calls to getNode will be very fast
def getNode(Foundry=None, Process=None):
  if Foundry is None and Process is None:
    return dict.keys() # all nodes
  elif Process is None:
    return list(foundry_lookup_table.get(Foundry,Set()))
  elif Foundry is None:
    return list(process_lookup_table.get(Process,Set()))
  else:
    return list(
foundry_lookup_table.get(Foundry,Set()).intersection(process_lookup_table.get(Process,Set()))




More information about the Python-list mailing list