1

I've been banging my head at a program which repeatedly chooses the largest item in a list and copies it over the entire list, adding a bit of variation each time. It feels like it should be easy, but one element ends up blank every time. Here's the code, with the important part separated:

import random as r
alice = []
bob = []
cathy= []
davos = []
ezekiel = []
francesco = []
graham = []
genVar = 0.25
classroom = [alice, bob, cathy, davos, ezekiel, francesco, graham]
for i in classroom:
    skill = r.random()
    i.append(skill)
    i.append(1-skill)
a = 0

while a < 10:
    best = max(classroom, key=lambda x:x[0]*x[1])
    for i in classroom:
        i.clear()
        i.extend(best)
        print(best)
        i[0] += r.uniform(-1*genVar, genVar)
        i[1] += r.uniform(-1*genVar, genVar)

    a+=1
    print(best)

When I run the cde, the error I get is like so:

[0.5598973199094259, 0.44010268009057407]
[0.5598973199094259, 0.44010268009057407]
[0.5598973199094259, 0.44010268009057407]
[]
Traceback (most recent call last):
  File "C:/Users/Leafy/AppData/Local/Programs/Python/Python36-32/multiplierlearner.py", line 24, in <module>
    i[0] += r.uniform(-1*genVar, genVar)
IndexError: list index out of range

The weird thing is, when I run the code a second time, I get this result instead:

[0.6459806429424123, 0.3540193570575877]
[]
Traceback (most recent call last):
  File "C:/Users/Leafy/AppData/Local/Programs/Python/Python36-32/multiplierlearner.py", line 23, in <module>
    i[0] += r.uniform(-1*genVar, genVar)
IndexError: list index out of range

The number of completed elements changes every time, and it's driving me insane. Any help is appreciated, and let me know if there's any more information I should put up.

Konoha
  • 11
  • 1
  • Since both ``i`` and ``best`` are in ``classroom``, it can occur that ``i is best`` which means ``i.clear()`` also clears ``best``. – MisterMiyagi May 22 '20 at 06:58
  • I see how i could be inside classroom, but isn't best its own separate variable outside of classroom? – Konoha May 22 '20 at 07:03
  • Does this answer your question? [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – MisterMiyagi May 22 '20 at 08:05
  • Might also be of interest: [If two variables point to the same object, why doesn't reassigning one variable affect the other?](https://stackoverflow.com/questions/56667280/if-two-variables-point-to-the-same-object-why-doesnt-reassigning-one-variable) and [Are python variables pointers? or else what are they?](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they/57380719#57380719) – MisterMiyagi May 22 '20 at 08:06

1 Answers1

0

A simple modification will be required as:

for i in classroom:
        if i != best:
            i.clear()
            i.extend(best)

I tried your code and modified it for better understanding,

import random as r
alice = []
bob = []
cathy= []
davos = []
ezekiel = []
francesco = []
graham = []
genVar = 0.25
classroom = [alice, bob, cathy, davos, ezekiel, francesco, graham]
for i in classroom:
    skill = r.randint(0,100)
    i.append(skill)
    i.append(skill-1)
a = 0

while a < 10:
    print("Iteration number",a)
    print()
    best = max(classroom, key=lambda x:x[1])
    print("best",best)

    print("max:",max(classroom, key=lambda x:x[1]))

    print("clasroom",classroom)
    for i in classroom:
        if i != best:
            i.clear()
            i.extend(best)
        print("best in iteration",best)
        i[0] += r.uniform(-1*genVar, genVar)
        i[1] += r.uniform(-1*genVar, genVar)

    a+=1
    print()
    print("best after iteration",best)
    print()
Prathamesh
  • 1,064
  • 1
  • 6
  • 16