Help building a dictionary of lists

NJ1706 nickj1706 at googlemail.com
Mon Nov 12 17:26:54 EST 2012


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 with the 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 CentOS 6.3



More information about the Python-list mailing list