Not a duplicate of this answer because that answer deals with permuting literals, and this question refers to objects. Please refer to this question for what happens when permuting objects.
Using python 2.
I need to generate all possible permutations of an object list that includes the empty list, and then all permutations of 1 object, then with 2 and so on.
I have found a way to do this with a string:
import itertools
stuff = [1, 2, 3]
for idx in range(len(stuff)+1):
for i in itertools.permutations('ABCD', idx):
print i
Sample output:
()
('A',)
('B',)
('C',)
('D',)
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'A')
.
.
.
.
('D', 'C', 'A')
('D', 'C', 'B')
What I need is to do this with a custom object list. I tried to apply the above algorithm to an objects list. My solution below:
originalObjectList = A list of many custom objects.
allPossibleOrderings=[]
for idx in range(len(originalObjectList)+1):
originalObjectListOrderings = list(itertools.permutations(originalObjectList, idx))
for objects in originalObjectListOrderings:
allPossibleOrderings.insert(0, map(copy.copy,objects))
The problem with this implementation is that the allPossibleOrderings list doesn't contain all permutations, only the last 29. It's as if when I insert new permutations, it overwrites old ones.