Help with a reverse dictionary lookup
Lonnie Princehouse
finite.automaton at gmail.com
Thu Mar 9 20:11:20 EST 2006
Here's the illegible gibberish version of your function. Once you
understand completely the following line of code, you will be well on
your way to Python nirvana:
getNodes = lambda Foundry=None,Process=None: [node for node,foundries
in dict.iteritems() if ((Foundry is None) and ((Process is None) or
(len([foundry for foundry,processes in foundries.iteritems() if Process
in processes]) > 0))) or ((Foundry in foundries) and ((Process is None)
or (Process in foundries[Foundry])))]
# ex:
getNodes(Foundry="umc", Process="2p6m_1.8-3.3_sal_ms")
# note: returns an empty list rather than None if no matching nodes
exist
# hint: You'll probably want to do rearrange your data structure or do
some caching if you have a large dataset. The dict-and-list solution
doesn't make this search very efficient.
And: I just noticed that len() won't work with a generator expression
as its argument. What's up with that?
More information about the Python-list
mailing list