Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help building a dictionary of lists

Reply
Thread Tools

Help building a dictionary of lists

 
 
NJ1706
Guest
Posts: n/a
 
      11-12-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 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
 
Reply With Quote
 
 
 
 
Thomas Bach
Guest
Posts: n/a
 
      11-13-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM



Advertisments