我在while循环中有以下代码。
if gender == 0 and len(men) < 51 : height = float((random.uniform(1.3, 1.9) + (random.randint(10, 20)/100.)).__format__('.2f')) weight = float((random.uniform(45, 100) * height).__format__('.2f')) attr['height'] = height attr['weight'] = weight men.append(attr)
因此,此代码始终提供一些随机高度和随机权重。但是异地循环(完成时)。如果这样做print men,我得到以下结果:
print men
[{'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}, {'weight': 76.64, 'height': 1.75}]
它总是一样。不过我的,如果不是使用attr[height] = height; attr['weight] = weight和使用men.append(height); men.append(weight)我得到以下结果:
attr[height] = height; attr['weight] = weight
men.append(height); men.append(weight)
print men [1.91, 145.95, 1.64, 95.66, 2.0, 159.94, 1.74, 143.36, 1.68, 97.99, 1.6, 90.11, 1.63, 116.2, 1.56, 96.8, 2.04, 198.56, 1.56, 145.96, 1.44, 67.57, 1.83, 94.97, 1.85, 175.69, 1.84, 101.84, 1.54, 135.0, 1.41, 101.23, 1.92, 167.59, 1.74, 142.55, 1.49, 129.07, 1.83, 161.28, 1.59, 97.16, 1.46, 134.53, 2.03, 158.72, 2.05, 184.43, 1.97, 162.81]
如果我在循环内打印attr,它总是具有不同的值(它是我想要的)。但是,当我将其追加到列表中时,列表的值始终相同。我究竟做错了什么?
当前,您的代码的简化示例可以更全面地 说明 结果 为何如此 :
all_items = [] new_item = {} for i in range(0,5): new_item['a'] = i new_item['b'] = i all_items.append(new_item) print new_item print hex(id(new_item)) # print memory address of new_item print all_items
请注意, 每次 循环 时, 对象的内存地址都相同。这意味着每次添加的对象都是相同的。因此,当您打印最终列表时,您将在循环中的每个位置打印相同对象的坐标。
每次您遍历循环时,值都会被更新- 假设您每天都在同一堵墙上绘画。第一天,可能是蓝色的。第二天,您重新粉刷相同的墙(或对象),然后变成绿色。最后一天,您将其绘制成橙色,然后变为橙色- 同一堵墙现在 总是 橙色。您对attr对象的引用就像说您有同一堵墙。
attr
即使您在涂完油漆后看着墙壁,颜色也会改变。但是之后是橙色的墙-即使您看了5次也是如此。
当我们在每次迭代中将对象设为新对象时,请注意发生了两件事:
这类似于绘制 不同的 墙。在完成最后一幅墙的绘制之后,先前的每面墙仍会以您首先绘制的颜色进行绘制。
您可以在下面的每次迭代中创建每个对象的地方看到它:
all_items = [] for i in range(0,5): new_item = {} new_item['a'] = i new_item['b'] = i all_items.append(new_item) print hex(id(new_item)) print all_items
您也可以采用其他方式,例如:
all_items = [] for i in range(0,5): new_item = {'a': i, 'b': i} all_items.append(new_item) print hex(id(new_item)) print all_items
甚至一步一步:
all_items = [] for i in range(0,5): all_items.append({'a': i, 'b': i}) print all_items
因此,以下任何一种都可以工作:
attr = {} attr['height'] = height attr['weight'] = weight men.append(attr)
要么:
men.append({'height': height, 'weight': weight})