python 上的列表总是附加相同的值 [英] List on python appending always the same value

查看:31
本文介绍了python 上的列表总是附加相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 while 循环中有以下代码.

I have the following code inside a while loop.

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,我会得到以下结果:

So this code always gives some random height and random weight. But outsite de loop (when it is finished). If I do print men, I get the following result:

[{'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) 我得到以下结果:

Its always the same thing. But, if I instead of using attr[height] = height; attr['weight] = weight and use men.append(height); men.append(weight) I get the following result:

印花男[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 它总是有不同的值(这是我想要的).但是当我将它附加到我的列表中时,我的列表中的值总是相同的.我做错了什么?

If I print attr inside the loop it always has a different value ( its what I want). But when I append it to my list, the values of my list are always the same. What am I doing wrong?

推荐答案

当前代码的简化示例,可更全面地解释为什么您的结果是这样的:

A simplified example of your code currently to explain more fully why your results are the way they are:

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

注意你的对象的内存地址是相同的每次你遍历你的循环.这意味着每次添加的对象都是相同的.因此,当您打印最终列表时,您正在打印循环中每个位置的同一对象的坐标.

Notice the memory address for your object is the same each time you go through your loop. This means that your object being added is the same, each time. So when you print the final list, you are printing the coordinates of the same object at every location in the loop.

每次循环时,值都会更新 - 想象一下您每天都在同一面墙上作画.第一天,它可能是蓝色的.第二天,您重新粉刷同一面墙(或物体),然后它变绿了.最后一天你把它涂成橙色,它是橙色的——同一面墙现在总是橙色.您对 attr 对象的引用就像在说您有相同的墙.

Each time you go through the loop, the values are being updated - imagine you are painting over the same wall every day. The first day, it might be blue. The next day, you repaint the same wall (or object) and then it's green. The last day you paint it orange and it's orange - the same wall is always orange now. Your references to the attr object are like saying you have the same wall.

即使您在粉刷墙壁后看着它,颜色也发生了变化.但后来它是一面橙色的墙——即使你看它 5 次.

Even though you looked at the wall after painting it, the color changed. But afterwards it is a orange wall - even if you look at it 5 times.

当我们在每次迭代中将对象作为一个新对象时,请注意发生了两件事:

When we make the object as a new object in each iteration, notice two things happen:

  1. 内存地址变化
  2. 值作为唯一值保留

这类似于粉刷不同的墙壁.刷完最后一堵墙后,之前的每一面墙都涂上了你第一次刷的颜色.

This is similar to painting different walls. After you finish painting the last one, each of the previous walls is still painted the color you first painted it.

您可以在以下内容中看到这一点,其中每次迭代都会创建每个对象:

You can see this in the following, where each object is created each iteration:

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

你也可以换一种方式,比如:

You can also do in a different way, such as:

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

因此,以下任一方法都有效:

Either of the following will therefore work:

attr = {}
attr['height'] = height 
attr['weight'] = weight

men.append(attr)

或:

men.append({'height': height, 'weight': weight})

这篇关于python 上的列表总是附加相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆