Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List on python appending always the same value [duplicate]

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)

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}]

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:

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]

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?

like image 785
Carlos Porta Avatar asked Mar 10 '16 02:03

Carlos Porta


2 Answers

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.

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.

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. the memory address changes
  2. the values persist as unique values

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

or even in one step:

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)

or:

men.append({'height': height, 'weight': weight})
like image 96
enderland Avatar answered Oct 18 '22 20:10

enderland


You are adding the same attr dictionary to the list several times. The following lines will only mutate the attr dictionary instead of creating a new one:

attr['height'] = height 
attr['weight'] = weight

You should create a new dict each time, such as:

attr = {'height': height, 'weight': weight}
like image 36
Selcuk Avatar answered Oct 18 '22 21:10

Selcuk