I have a tuple of Action objects
I want to create a list of every possible permutation of the Action objects N deep.
i.e. if I have the actions Eat, Sleep, Drink, and N = 2
the list would be
[[Eat, Eat],
[Eat, Sleep],
[Eat, Drink],
[Sleep, Eat],
[Sleep, Sleep],
[Sleep, Drink],
[Drink, Eat],
[Drink, Sleep],
[Drink, Drink]]
Now, I have a much larger list than three Actions, and N will probably be 3 or more.
How would I do this in Python 2.7?
Sounds like you want the Cartesian product of a list with itself. Use
itertools.product():The optional
repeatargument controls how “deep” your list is.Beware that the size of the list grows exponentially with the depth
N. Don’t materialise the whole list at once – instead, use one element at a time.