I'm trying to run a program that goes something like this:
newlist = oldlist
for n in range(100):
#run some stuff to try and improve oldlist over the 100 times its looped though randomized algorithm
if oldlist better than newlist:
newlist = oldlist
return newlist
What I want to accomplish is to constantly updating "newlist" if my program managed to improve on the oldlist that is being constantly modified for 100 times though randomized algorithm.
This concept works if I'm working with variables but not lists presumably because of the way Python assign names to lists while not actually copying the list. When the program returns the newlist it is still the newlist as defined by outside the for-loop.
In complete frustration I decided to just change the newlist as well, in the following way:
newlist = list(oldlist) #having to do this is already pretty weird tbh...
for n in range(100):
#run some stuff to try and improve oldlist over the 100 times its looped though randomized algorithm
if oldlist better than newlist:
for index, item in enumerate(oldlist):
newlist[index] = item
return newlist
But that's such a round-a-bout way of doing things and it makes me sad, any thoughts on this/better way to do this?
Also what's the benefit that comes from assigning two names to the same list when I didnewlist = oldlist
instead of just making a copy automatically?