Dictionaries -- ahh the fun.. (newbie help)

Scott David Daniels scott.daniels at acm.org
Tue May 9 19:10:49 EDT 2006


rh0dium wrote:
> Hi all,
> 
> Can someone help me out.  I am trying to determing for each run whether
> or not the test should pass or fail but I can't seem to access the
> results ..
> 
> Alternatively can someone suggest a better structure ( and a lesson as
> to the reasoning ) that would be great too!!

Here's a little less convoluted version:

     cells = dict(NOR3X1=dict(lvs='pass', drc='fail'),
                  OR3X=dict(lvs='pass'),
                  AND3X1=dict(drc='fail'))

     for cell in cells:
         print cell
         for run, expect in cells[cell].items():
             print cell, run, "should", expect

Or even:

     for cell, expectations in cells.items():
         print cell
         for run, expect in expectations.items():
             print cell, run, "should", expect

You might prefer .iteritems rather than .items if the lists get huge.

* I use the dict(name=val [, name=val)*) form if the keys are symbols.
* Don't get any more indirect than you need to.
* A list of single-entry dictionaries is almost never a good idea;
   if you mean a list of pairs, use that.  if you mean a dictionary
   with a number of keys, use that.

-- 
-Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list