selcuk egese
selcuk egese

Reputation: 49

Getting all the possible combinations within 3 different sets

I am trying to write a python program that goes through all the possible list of number for 3 different sets and prints out all the possible numbers. At the bottom the range of the numbers are given. No 2 sets of numbers should repeat each order should be different than the other. Please take a look at the expected output for the expected output for the code. I want the code below to go back to for i in set1: after the for p in set3: is out of index so it could print out all the possible combinations.

set1 = [i for i in range(2,11)]
set2 = [i for i in range(1,5)]
set3 = [i for i in range(1,5)]

for i in set1:
    for k in set2:
        for p in set3:
            print(set1[i], set2[k], set3[p])

Set 1: numbers from 2 to 11

Set 2: numbers from 1 to 5

Set 3: number from 1 to 5

Expected Results(Set 1, Set 2, Set 3):

(2,1,1) , (2,2,1), (2,3,1), (2,4,1), (2,1,2), (2,1,3), (2,1,4)

(2,2,2), (2,2,3), (2,2,4), (1,2,1), (3,2,1), (4,2,1), (5,2,1,), (6,2,1),(7,2,1), (8,2,1), (9,2,1), (10,2,1)

Upvotes: 0

Views: 1026

Answers (1)

Viraat Das
Viraat Das

Reputation: 38

you can use itertools.product

import itertools
list_set = [set1, set2, set3]
for element in itertools.product(*list_set):
  print(element)

Upvotes: 1

Related Questions