<p>造成混淆的原因是<code>y.append(z)</code>行。你知道吗</p>
<p>这不会附加字典<code>z</code>的<em>副本,而是另一个对<code>z</code>的相同实例的引用。你知道吗</p>
<p>因此,在循环的最后一次迭代中,当您修改刚刚附加的字典时,您也在修改上一次迭代中附加的第一个字典。你的列表<code>y</code>看起来就像<code>[z, z]</code>。你知道吗</p>
<pre><code>>>> print y
[{'streak': 0, 'hits': 0, 'name': 'none', 'player': 2, 'misses': 0, 'longest streak': 0, 'fired': 0}, {'streak': 0, 'hits': 0, 'name': 'none', 'player': 2, 'misses': 0, 'longest streak': 0, 'fired': 0}]
>>> y[0]
{'fired': 0,
'hits': 0,
'longest streak': 0,
'misses': 0,
'name': 'none',
'player': 2,
'streak': 0}
>>> y[0] is y[1]
True
>>> y[0]['player'] = 123
>>> y[1]['player']
123
</code></pre>
<p>创建结构的一种更具python风格的方法是使用列表理解:</p>
<pre><code>>>> def new_player(n, name="none"):
... return {"player" : n, "name": name, "fired": 0, "hits": 0, "misses": 0, "streak": 0, "longest streak": 0}
...
>>> y = [new_player(n) for n in range(2)]
>>> y
[{'fired': 0,
'hits': 0,
'longest streak': 0,
'misses': 0,
'name': 'none',
'player': 0,
'streak': 0},
{'fired': 0,
'hits': 0,
'longest streak': 0,
'misses': 0,
'name': 'none',
'player': 1,
'streak': 0}]
</code></pre>