Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a dictionary

38 views
Skip to first unread message

arg...@gmail.com

unread,
Oct 9, 2012, 7:59:49 AM10/9/12
to
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...

Steven D'Aprano

unread,
Oct 9, 2012, 9:00:46 AM10/9/12
to
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')



--
Steven

Roy Smith

unread,
Oct 9, 2012, 9:08:02 AM10/9/12
to
In article <50741ffe$0$6574$c3e8da3$5496...@news.astraweb.com>,
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.

Ulrich Eckhardt

unread,
Oct 9, 2012, 9:11:25 AM10/9/12
to
Am 09.10.2012 13:59, schrieb arg...@gmail.com:
> 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

0 new messages