-2

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]?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Blake
  • 11
  • 1
  • 3
    What is expected output & please include what you have tried so far. – sushanth Sep 01 '22 at 03:52
  • 1
    This is not what the word "permutation" typically means. Based on what you've given us, I think you're either looking for [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) or [the `zip` function](https://stackoverflow.com/questions/13704860/zip-lists-in-python), and I can't tell which right now. – Silvio Mayolo Sep 01 '22 at 03:57
  • You're looking to generate 1- through k-tuples of value pairs, with the first value in the pair ranging from 1 through m and the second value ranging from 1 through n, containing every possible combination of values? (the format you're asking for would then be those tuples of pairs unzipped into two lists - but that's trivial, and it's quite likely you'll want to process them in pairs anyway) – Grismar Sep 01 '22 at 03:57
  • 1
    "that returns something from the [1], [1] up to say [15,15,15,15,15], [n,n,n,n,n]?" I can't understand what this means. The question is almost certainly a duplicate, but I can't tell **which** question it duplicates without a proper specfication. For the given input `color_list, shape_list = [15,9,7], [1,3,7]`, please show the **exact, complete** desired output. I can't tell you how to make "all the permutations" (assuming that is the correct word, which it might not be) because right now I can't tell what you expect *one* of them to look like, nor can I tell *how many* you expect here. – Karl Knechtel Sep 01 '22 at 04:01
  • 1
    "I have 15 colors and let's say a recursively infinite number of shapes" I can't understand this. "Recursively infinite" does not make any sense at all. I also need an explanation of how an integer is intended to represent either a colour or a shape. – Karl Knechtel Sep 01 '22 at 04:03
  • Also, the number of sides of a regular N-gon is N, not infinity. If N happens to equal infinity, we usually call that shape a "circle". – Silvio Mayolo Sep 01 '22 at 04:08

2 Answers2

0

IIUC, you are trying to do the following -

  1. Create color, shape combinations with some lower and upper limit for each (color = 3, shape = 5), (color = 1, shape = 99) etc.
  2. 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 -

  1. Create itertools.product using ranges for color and shape. This gives you all possible (color, shape) tuples.

  2. For permutation order 1 to m (1, 2, 3, ..., m) permute over this list of all possible tuples k times where 1>=k>=m

  3. 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)]

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • Glad to help. Asking a good question on SO takes a bit of time to learn as well, so don't feel discouraged. A few downvotes don't matter as you can very quickly reverse them by either improving your question or just constant participation on the platform. A good way to start would be to read [ask] and [mre]. Please take all the feedback constructively and very soon you will find yourself very comfortable on SO! Good luck! – Akshay Sehgal Sep 04 '22 at 23:37
0

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)))
Grismar
  • 27,561
  • 4
  • 31
  • 54