Skip to content Skip to sidebar Skip to footer

List On Python Appending Always The Same Value

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')

Solution 1:

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 inrange(0,5):
    new_item['a'] = i
    new_item['b'] = i

    all_items.append(new_item)
    print new_item
    printhex(id(new_item))  # print memory address of new_itemprint 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 inrange(0,5):
    new_item = {}
    new_item['a'] = i
    new_item['b'] = i

    all_items.append(new_item)
    printhex(id(new_item))


 print all_items

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

all_items = []
for i inrange(0,5):
    new_item = {'a': i, 'b': i}
    all_items.append(new_item)
    printhex(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})

Solution 2:

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}

Solution 3:

To avoid the ambiguity involved in re-initializing (a step you missed) a variable in a loop and appending it to a list, Python allows you to be much more succinct.

height = float((random.uniform(1.3, 1.9) + (random.randint(10, 20)/100.)).__format__('.2f'))
men.append({
    'height': height,
    'weight': float((random.uniform(45, 100) * height).__format__('.2f')),
})

Solution 4:

Your problem is that you are mutating a dict and appending that same dict over and over. You should create a new dict each time

It's also much clearer to use round instead of convert to str and back to float

if gender == 0andlen(men) < 51 :
    height = round(random.uniform(1.3, 1.9) + (random.randint(10, 20)/100.), 2)
    weight = round(random.uniform(45, 100) * height, 2)
    men.append({'height' : height, 'weight', weight})

Solution 5:

you should Use the definition of dictionary inside the for loop instead of outside the for loop

for i in range(0, 10):
    attr = {}
    ifgender== 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)

This gives me following output:

[{'weight': 126.75, 'height': 1.76}, {'weight': 155.35, 'height': 1.91}, {'weight': 169.2, 'height': 1.87}, {'weight': 135.54, 'height': 1.45}, {'weight': 98.58, 'height': 1.98}, {'weight': 133.73, 'height': 1.44}, {'weight': 149.48, 'height': 1.87}, {'weight': 121.93, 'height': 1.46}, {'weight': 160.09, 'height': 1.93}, {'weight': 115.62, 'height': 1.56}]

Post a Comment for "List On Python Appending Always The Same Value"