Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33205 > unrolled thread
| Started by | NJ1706 <nickj1706@googlemail.com> |
|---|---|
| First post | 2012-11-12 14:26 -0800 |
| Last post | 2012-11-13 14:53 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Help building a dictionary of lists NJ1706 <nickj1706@googlemail.com> - 2012-11-12 14:26 -0800
Re: Help building a dictionary of lists Thomas Bach <thbach@students.uni-mainz.de> - 2012-11-13 14:53 +0100
| From | NJ1706 <nickj1706@googlemail.com> |
|---|---|
| Date | 2012-11-12 14:26 -0800 |
| Subject | Help building a dictionary of lists |
| Message-ID | <9f44d1de-bccf-41ad-8529-ff8a65d7e2b4@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Thomas Bach <thbach@students.uni-mainz.de> |
|---|---|
| Date | 2012-11-13 14:53 +0100 |
| Message-ID | <mailman.3626.1352814915.27098.python-list@python.org> |
| In reply to | #33205 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web