Creating a dictionary

Discussion in 'Python' started by argbsk@gmail.com, Oct 9, 2012.

  1. Guest

    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...
    , Oct 9, 2012
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Oct 9, 2012
    #2
    1. Advertising

  3. Roy Smith Guest

    In article <50741ffe$0$6574$c3e8da3$>,
    Steven D'Aprano <> 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.
    Roy Smith, Oct 9, 2012
    #3
  4. Am 09.10.2012 13:59, schrieb :
    > 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
    Ulrich Eckhardt, Oct 9, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    416
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    301
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    2,944
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    500
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    165
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page