On 12 November 2012 22:26, NJ1706 <span dir="ltr"><<a href="mailto:nickj1706@googlemail.com" target="_blank">nickj1706@googlemail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


Chaps,<br>
<br>
I am new to Python & have inherited a test harness written in the language that I am trying to extend.<br>
<br>
The following code shows how dictionaries holding lists of commands are handled in the script...<br>
<br>
>>> Start of Code_1 <<<</blockquote><div><SNIP> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


>>> End of Output_1 <<<<br>
<br>
Note that dict1 contains only the details of the particlare test, see YYY.<br>
<br>
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.<br>



<br>
After some reading around I found the dict.fromkeys() method & came up with the following...<br>
<br>
>>> Start of Code_2 <<<</blockquote><div> <SNIP </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


>>> End of Ouput_2 <<<<br>
<br>
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.<br>



<br>
I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on CentOS 6.3<br></blockquote><div><br></div><div>First of all, thank you for being thorough. It's always a help. *However*, a minimalistic will be read faster usually, so you may want to crop down what you have. Additionally, it can result in you solving it yourself!</div>

<div><br></div><div>The problem is as follows</div><div><br></div><div>"{1:[], 2:[], 3:[]}" does not equal "dict.fromkeys([1, 2, 3], [])"</div><div><br></div><div>You could have worked out this much very quickly, as it is the only thing you change in the code.</div>

<div><br></div><div>Let's look:</div><div><div>>>> {1:[], 2:[], 3:[]}</div><div>{1: [], 2: [], 3: []}</div><div>>>> dict.fromkeys([1,2,3], [])</div><div>{1: [], 2: [], 3: []}</div><div>>>> </div>

</div><div><br></div><div>Oh no! They are the same! What has happened?</div><div>Well, they're *not*. In the first example each of the lists are *unique*:</div><div><br></div><div><div>>>> dct = {1:[], 2:[], 3:[]}</div>

<div>>>> dct[1] is dct[2]</div><div>False</div><div>>>> dct[1] is dct[3]</div><div>False</div><div>>>> dct[2] is dct[3]</div><div>False</div></div><div>>>></div><div><br></div><div>In the second, they are all *the same list*:</div>

<div><br></div><div><div>>>> dct = dict.fromkeys([1,2,3], [])</div><div>>>> dct[1] is dct[2]</div><div>True</div><div>>>> dct[1] is dct[3]</div><div>True</div><div>>>> dct[2] is dct[3]</div>

<div>True</div><div>>>> </div></div><div><br></div><div>What does this mean?</div><div><br></div><div><div>>>> a = []</div><div>>>> b = []</div><div>>>> c = b</div><div>>>> </div>

<div>>>> a is b</div><div>False</div><div>>>> b is c</div><div>True</div><div>>>> </div><div>>>> a.append(1)</div><div>>>> b.append(2)</div><div>>>> c.append(3)</div>
<div>
>>> </div><div>>>> a</div><div>[1]</div><div>>>> b</div><div>[2, 3]</div><div>>>> c</div><div>[2, 3]</div><div>>>> </div></div><div><br></div><div>Because b and c in this are *the same*, changing one *is* changing the other. Hence, you've appended *two* things to b and c, but only one to a.</div>

<div><br></div><div>In your code you use .append on the items in your dict. This will have the same problem as with a, b and c above.</div><div><br></div><div>If you want to make a dict, you can use a dictionary comprehension. I assume you understand what a list comprehension is. If not, look here:</div>

<div><a href="http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions">http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions</a><br></div><div><br></div><div>Dict comprehension:</div>

<div>{i:[] for i in ["Test 1", "Test 2", "Test 3"]}</div></div></div>