Help building a dictionary of lists


N

NJ1706

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...
#! /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]
This code produces the following output
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
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...

#! /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]

This produces the following output
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
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
 
Ad

Advertisements

T

Thomas Bach

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.
 

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

Top