Creating a dictionary

Roy Smith roy at panix.com
Tue Oct 9 09:08:02 EDT 2012


In article <50741ffe$0$6574$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> On Tue, 09 Oct 2012 04:59:49 -0700, argbsk wrote:
> 
> > below is the text file i have How to create Facility as a key and then
> > assign multiple values to it
> 
> To use Facility as a key in a dict:
> 
> d = {}
> d['Facility'] = 'ham'
> 
> Note that keys are case-sensitive, so that 'Facility', 'facility', 
> 'FACILITY' and 'FaCiLiTy' are all different.
> 
> To have multiple values assigned to a key, use a list:
> 
> d['Facility'] = ['ham', 'spam', 'cheese', 'eggs']
> d['Facility'].append('tomato')

I think what he really wants to end up with is a dictionary of 
dictionaries:

{'BACKUP': {'Total' : 34,
         'Passed' : 32,
         'Failed' : 2,
         'Not Run' : 0
      },
 'CDU': {....}
}

Or, somewhat more work, but a richer solution, create a FacilityData 
class, whose attributes are total, passed, failed, not_run, etc, and 
have instances of FacilityData be the values.



More information about the Python-list mailing list