Let's say I start with these two hypothetical lists: color and shape.
I have 15 colors and let's say a recursively infinite number of shapes (like number of sides of a regular ngon). At its most basic it starts like
color_list, shape_list = [1], [1]
up to the complexity I mentioned, one example of length 3 might be
color_list, shape_list = [15,9,7], [1,3,7]
These numbers are arbitrary to compare one particular permutation that combines color 15 with shape 1, color 9 with shape 3, color 7 with shape 7.
How would I create an iteration over every possible permutation that returns something from the [1], [1]
up to say [15,15,15,15,15], [n,n,n,n,n]
?
CodePudding user response:
IIUC, you are trying to do the following -
- Create color, shape combinations with some lower and upper limit for each (color = 3, shape = 5), (color = 1, shape = 99) etc.
- Then you are trying to permute over these color combinations for each permutation order from 1 to m (in your example m = 5)
Here is a piece of code that should get you something similar. Please do note, this will explode exponentially, so avoid larger limits.
import itertools
color_i = 1
color_n = 5
shape_i = 1
shape_n = 6
permutes = 3
# color, shape combos
combos = list(itertools.product(range(color_i, color_n 1), range(shape_i, shape_n 1)))
# create permutations from the color, shape combos
permutations = []
for p in range(permutes):
for i in itertools.permutations(combos,r=p 1):
permutations.append(list(zip(*i)))
permutations
[[(1,), (1,)],
[(1,), (2,)],
[(1,), (3,)],
[(1,), (4,)],
[(1,), (5,)],
[(1,), (6,)],
[(2,), (1,)],
[(2,), (2,)],
[(2,), (3,)],
[(2,), (4,)],
[(2,), (5,)],
[(2,), (6,)],
[(3,), (1,)],
[(3,), (2,)],
[(3,), (3,)],
[(3,), (4,)],
[(3,), (5,)],
[(3,), (6,)],
[(4,), (1,)],
[(4,), (2,)],
[(4,), (3,)],
[(4,), (4,)],
[(4,), (5,)],
[(4,), (6,)],
[(5,), (1,)],
[(5,), (2,)],
[(5,), (3,)],
[(5,), (4,)],
[(5,), (5,)],
[(5,), (6,)],
[(1, 1), (1, 2)],
[(1, 1), (1, 3)],
...,
[(1, 4), (1, 2)],
[(1, 4), (1, 3)],
[(1, 4), (1, 4)],
[(1, 4), (1, 5)],
[(1, 4), (1, 6)],
[(1, 5), (1, 1)],
[(1, 5), (1, 2)],
[(1, 5), (1, 3)],
[(1, 5), (1, 4)],
[(1, 5), (1, 5)],
[(1, 5), (1, 6)],
[(1, 1), (2, 1)],
[(1, 1), (2, 3)],
[(1, 1), (2, 4)],
[(1, 1), (2, 5)],
[(1, 1), (2, 6)],
[(1, 2), (2, 1)],
[(1, 2), (2, 2)],
...,
[(4, 4), (3, 6)],
[(4, 5), (3, 1)],
[(4, 5), (3, 2)],
[(4, 5), (3, 3)],
[(4, 5), (3, 4)],
[(4, 5), (3, 5)],
[(4, 5), (3, 6)],
[(4, 1), (4, 1)],
[(4, 1), (4, 2)],
[(4, 1), (4, 3)],
[(4, 1), (4, 4)],
[(4, 1), (4, 5)],
...,
[(5, 5), (6, 2)],
[(5, 5), (6, 3)],
[(5, 5), (6, 4)],
[(5, 5), (6, 5)],
[(1, 1, 1), (1, 2, 3)],
[(1, 1, 1), (1, 2, 4)],
[(1, 1, 1), (1, 2, 5)],
[(1, 1, 1), (1, 2, 6)],
[(1, 1, 2), (1, 2, 1)],
[(1, 1, 2), (1, 2, 2)],
[(1, 1, 2), (1, 2, 3)],
...,
[(1, 1, 4), (1, 2, 6)],
[(1, 1, 5), (1, 2, 1)],
[(1, 1, 5), (1, 2, 2)],
[(1, 1, 5), (1, 2, 3)],
[(1, 1, 5), (1, 2, 4)],
[(1, 1, 5), (1, 2, 5)],
[(1, 1, 5), (1, 2, 6)],
[(1, 1, 1), (1, 3, 2)],
[(1, 1, 1), (1, 3, 4)],
[(1, 1, 1), (1, 3, 5)],
[(1, 1, 1), (1, 3, 6)],
[(1, 1, 2), (1, 3, 1)],
[(1, 1, 2), (1, 3, 2)],
[(1, 1, 2), (1, 3, 3)],
[(1, 1, 2), (1, 3, 4)],
[(1, 1, 2), (1, 3, 5)],
[(1, 1, 2), (1, 3, 6)],
[(1, 1, 3), (1, 3, 1)],
[(1, 1, 3), (1, 3, 2)],
[(1, 1, 3), (1, 3, 3)],
[(1, 1, 3), (1, 3, 4)],
[(1, 1, 3), (1, 3, 5)],
[(1, 1, 3), (1, 3, 6)],
[(1, 1, 4), (1, 3, 1)],
[(1, 1, 4), (1, 3, 2)],
[(1, 1, 4), (1, 3, 3)],
[(1, 1, 4), (1, 3, 4)],
[(1, 1, 4), (1, 3, 5)],
[(1, 1, 4), (1, 3, 6)],
[(1, 1, 5), (1, 3, 1)],
[(1, 1, 5), (1, 3, 2)],
[(1, 1, 5), (1, 3, 3)],
[(1, 1, 5), (1, 3, 4)],
[(1, 1, 5), (1, 3, 5)],
[(1, 1, 5), (1, 3, 6)],
[(1, 1, 1), (1, 4, 2)],
[(1, 1, 1), (1, 4, 3)],
[(1, 1, 1), (1, 4, 5)],
[(1, 1, 1), (1, 4, 6)],
[(1, 1, 2), (1, 4, 1)],
[(1, 1, 2), (1, 4, 2)],
[(1, 1, 2), (1, 4, 3)],
[(1, 1, 2), (1, 4, 4)],
[(1, 1, 2), (1, 4, 5)],
[(1, 1, 2), (1, 4, 6)],
[(1, 1, 3), (1, 4, 1)],
[(1, 1, 3), (1, 4, 2)],
[(1, 1, 3), (1, 4, 3)],
[(1, 1, 3), (1, 4, 4)],
[(1, 1, 3), (1, 4, 5)],
[(1, 1, 3), (1, 4, 6)],
...]
How it works -
Create
itertools.product
using ranges for color and shape. This gives you all possible (color, shape) tuples.For permutation order 1 to m (1, 2, 3, ..., m) permute over this list of all possible tuples k times where 1>=k>=m
Use
list(zip(*i))
on each generated permutation to transpose it from[(color1, shape1), (color2, shape2), (color3, shape3)]
to[(color1, color2, color3), (shape1, shape2, shape3)]
CodePudding user response:
You have a number of parameters:
- the number of possible colours (for example, 2)
- the number of possible shapes (for example, 3)
- the number of colour / shape combinations (for example, 4)
In Python:
n_colours = 2
colours = range(1, n_colours 1)
n_shapes = 3
shapes = range(1, n_shapes 1)
# creating a list of all possible combinations of colours and shapes:
all_pairs = list(product(colours, shapes))
You asked to get the combinations in two separate lists of the colours and the corresponding shapes. It seems more sensible to generate them as tuples of pairs instead:
from itertools import product
max_len = 4
tuples = [t for p in [product(all_pairs, repeat=n) for n in range(1, max_len 1)] for t in p]
But if you wanted to generate them as pairs of separate colours and shapes instead, here's an implementation that generates them one after the other:
from itertools import product
def all_colour_shape_pairs(n_colours, n_shapes, max_len):
ps = list(product(range(1, n_colours 1), range(1, n_shapes 1)))
for t in [t for p in [product(ps, repeat=n) for n in range(1, max_len 1)] for t in p]:
yield zip(*t)
result = all_colour_shape_pairs(2, 3, 4)
while input('Press enter for another, or enter "x" to stop:') != 'x':
print(tuple(next(result)))
Output
((1,), (1,))
((1,), (2,))
((1,), (3,))
((2,), (1,))
...
((2,), (3,))
((1, 1), (1, 1))
((1, 1), (1, 2))
...
((2, 2), (3, 3))
((1, 1, 1), (1, 1, 1))
((1, 1, 1), (1, 1, 2))
...
And if you want the generator to keep generating results "inifinitely" (or rather, to stop when the computer runs out of resources and is unable to continue):
from itertools import product
def all_colour_shape_pairs(n_colours, n_shapes, max_len):
ps = list(product(range(1, n_colours 1), range(1, n_shapes 1)))
n = 1
while True:
for t in product(ps, repeat=n):
yield zip(*t)
n = 1
result = all_colour_shape_pairs(2, 3, 4)
while input('Press enter for another, or enter "x" to stop:') != 'x':
print(tuple(next(result)))