0
votes

Using Python 2.7.

I have four different custom "Airplane" objects in a list, each with their own unique attributes.

Per my requirements, I need to generate all possible permutations of airplane orderings:

    # airplaneList = list of 4 airplane objects
    airplaneListOrderings = list(itertools.permutations(airplaneList))

Per each permutation, I run the permuted list through my program and change airplane attributes in the process, say:

for permutation in airplaneListOrderings:
    for plane in permutation:
        if plane.fuel = 0:
            plane.state = "landing"
        else:
            plane.state = "waiting"

The problem is, that when I finish with the first permutation and start the second permutation, this second permutation saves the states of the first permutation. That is, in the second permutation, the plane that has 0 fuel is already landing.

Am I permuting object references? If so, I need rearrange the actual objects.

How do I create a list of permutations so that if I change the objects in one permutation, it does not change any other permutation?

1
It is because the element in the second permutation is of type object which is still referring to the object in the first permutation. So with each iteration first copy the object pairs consisting of NEW objectsmad_
"Am I permuting object references? If so, I need rearrange the actual objects." There is no distinction in python. You are permuting a list of objects. Those objects are never copied, so if you change one, those changes will be reflected in any reference to that object in your programjuanpa.arrivillaga

1 Answers

1
votes

You can create copies of objects

airplaneListOrderingsnew =[]
import copy
for objects in airplaneListOrderings:
    airplaneListOrderingsnew.append(map(copy.copy,objects))