Creating a dictionary

A

argbsk

below is the text file i have How to create Facility as a key and then assign multiple values to it

FACILITY : BACKUP
FAILED BACKUP_BEFORE
FAILED BACKUP_INTERCHANGE
Total : 34
Passed : 32
Failed : 2
Not Run : 0

FACILITY : CDU
Total : 9
Passed : 9
Failed : 0


for example Facility BACKUP is a key & its values are Total Passed, Failed Not Run and Facility CDU as a key & its values Total:9,Passed:9,Failed:0 etc...
 
S

Steven D'Aprano

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')
 
R

Roy Smith

Steven D'Aprano said:
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.
 
U

Ulrich Eckhardt

Am 09.10.2012 13:59, schrieb (e-mail address removed):
below is the text file i have How to create Facility as a key and then assign multiple values to it

The value part of a dict element can be any kind of object, like e.g. a
tuple, namedtuple or even a dict.


Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top