Help building a dictionary of lists

Discussion in 'Python' started by NJ1706, Nov 12, 2012.

  1. NJ1706

    NJ1706 Guest

    Chaps,

    I am new to Python & have inherited a test harness written in the language that I am trying to extend.

    The following code shows how dictionaries holding lists of commands are handled in the script...

    >>> Start of Code_1 <<<

    #! /usr/bin/python

    # List of tests
    TestList = (
    'Test_1',
    'Test_2'
    )

    # Initialise the dictionary of lists
    dict1 = {
    'Test_1' : [],
    'Test_2' : [],
    }


    instances = ('1')

    # Loop through the list of tests
    for Test in TestList:
    print
    print "Test: ", Test

    # Append to the list for each instance
    for instance in instances:
    print " instance: ", instance

    # Initialise our string list
    str_l = []

    # Build string list
    str_l.append ('ID %s' % Test)
    str_l.append (' instance %s' % instance)

    # Convert to string
    str = ''.join (str_l)

    print " str: ", str

    # Assign to target list
    dict1[Test].append('%s' % str)

    print " dict1: ", dict1[Test]
    >>> End of Code_1 <<<


    This code produces the following output

    >>> Start of Output_1 <<<

    Test: Test_1
    instance: 1
    str: ID Test_1 instance 1
    dict1: ['ID Test_1 instance 1']

    Test: Test_2
    instance: 1
    str: ID Test_2 instance 1
    dict1: ['ID Test_2 instance 1'] # YYY
    >>> End of Output_1 <<<


    Note that dict1 contains only the details of the particlare test, see YYY.

    This is a very cut down script compared to the real thing & in reality there are many more entries in the TestList and also there are many dictionaries. To make the script simpler to extend I would like to remove the need to manually create each of the dictionaries.

    After some reading around I found the dict.fromkeys() method & came up withthe following...

    >>> Start of Code_2 <<<


    #! /usr/bin/python

    TestList = (
    'Test_1',
    'Test_2'
    )

    dict2 = dict.fromkeys (TestList, [])

    instances = ('1')


    for Test in TestList:
    print
    print "Test: ", Test


    for instance in instances:
    print " instance: ", instance

    # Initialise our string list
    str_l = []

    # Build string list
    str_l.append ('ID %s' % Test)
    str_l.append (' instance %s' % instance)

    # Convert to string
    str = ''.join (str_l)

    print " str: ", str

    # Assign to target list
    dict2[Test].append('%s' % str)

    print " dict2: ", dict2[Test]

    >>> End of Code_2 <<<


    This produces the following output

    >>> Start of Ouput_2 <<<

    Test: Test_1
    instance: 1
    str: ID Test_1 instance 1
    dict2: ['ID Test_1 instance 1']

    Test: Test_2
    instance: 1
    str: ID Test_2 instance 1
    dict2: ['ID Test_1 instance 1', 'ID Test_2 instance 1'] # XXX
    >>> End of Ouput_2 <<<


    This almost does what I want but dict2[Test_2] displayed at XXX contains the value for Test_1 as well as Test_2. I would be very grateful if someone can help me to get the line marked with XXX to be the same as YYY in code_1 at the start.

    I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on CentOS6.3
     
    NJ1706, Nov 12, 2012
    #1
    1. Advertising

  2. NJ1706

    Thomas Bach Guest

    On Mon, Nov 12, 2012 at 11:41:59PM +0000, Joshua Landau wrote:
    >
    > Dict comprehension:
    > {i:[] for i in ["Test 1", "Test 2", "Test 3"]}


    In Python 2.6 this syntax is not supported. You can achieve the same
    there via

    dict((i, []) for i in ['Test 1', 'Test 2', 'Test 3'])

    Also have a look at ``collections.defaultdict``.

    Regards,
    Thomas.
     
    Thomas Bach, Nov 13, 2012
    #2
    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:
    487
    Ilias Lazaridis
    Feb 21, 2006
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    451
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. james_027
    Replies:
    1
    Views:
    365
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Navkirat Singh
    Replies:
    6
    Views:
    3,348
    Navkirat Singh
    Jul 29, 2010
  5. Chris Weisiger

    Help with sorting lists of lists

    Chris Weisiger, Oct 14, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    148
    Tad McClellan
    Oct 14, 2004
Loading...

Share This Page