0

I just can't come up with a way to solve my problem: x is an integer. I want a list of all possibles combinations of x-tuples where those tuples' elements are in range from 0 to x (excluding x).

So if x = 3 I have 3^3 combinations: [(0,0,0),(0,0,1),(0,0,2),(0,1,0),(0,1,1),(0,1,2),(0,2,0),(0,2,1),(0,2,2),(1,0,0),(1,0,1),(1,0,2),(1,1,0),(1,1,1),(1,1,2),(1,2,0),(1,2,1),(1,2,2),(2,0,0),(2,0,1),(2,0,2),(2,1,0),(2,1,1),(2,1,2),(2,2,0),(2,2,1),(2,2,2)].

If x = 4 I would have 4^4 combinations with 4-tuples where the elements of those tuples are in {0,1,2,3}.

Marcel Braasch
  • 1,083
  • 1
  • 10
  • 19
  • Those are called *permutations* https://docs.python.org/3/library/itertools.html#itertools.permutations – tevemadar May 26 '19 at 20:53
  • 2
    Possible duplicate of [How to generate all permutations of a list in Python](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python) – Sheldore May 26 '19 at 20:57
  • No, this has nothing to do with permutations, thanks anyways :D – Marcel Braasch May 26 '19 at 21:00
  • Read the first answer [here](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python). It will solve your problem. The answer is: `import itertools` and then `answer = [i for i in itertools.product(range(3), repeat=3)]` – Sheldore May 26 '19 at 21:01
  • ok yea, it does solve my problem. Thank you, I will read into this more! – Marcel Braasch May 26 '19 at 21:02

3 Answers3

2

Here's the proper way to use itertools to get what you want:

list(itertools.product(range(3), repeat=3))

The output is:

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1),
 (0, 1, 2), (0, 2, 0), (0, 2, 1), (0, 2, 2), (1, 0, 0),
 (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2),
 (1, 2, 0), (1, 2, 1), (1, 2, 2), (2, 0, 0), (2, 0, 1),
 (2, 0, 2), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 2, 0),
 (2, 2, 1), (2, 2, 2)]

Of course, this can scale up by using values other than 3. In general:

list(itertools.product(range(x), repeat=x))

will work for any x.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
1

I think it's just a list comprehension:

mylist = [(x,y,z) for x in range(3) for y in range(3) for z in range(3)]

Note that using itertools.permutations(range(3)) doesn't generate duplicates, just the permutations of the set (0, 1, 2). I.e. you won't get (1, 1, 2), etc.

neutrino_logic
  • 1,289
  • 1
  • 6
  • 11
0

Ok, not permatations, but permutations with repeats perhaps.
Anyway, itertools.product() is doing that:

list(itertools.product([0,1,2],repeats=3))

result:

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (0, 2, 1), (0, 2, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 2, 0), (2, 2, 1), (2, 2, 2)]

oh it's a dupe. But I found it too :-)

(Side remark: combinations are about subsets, and thus order of elements does not matter to them)

tevemadar
  • 12,389
  • 3
  • 21
  • 49