1

Let's say I have the following arrays of strings:

Background = {"Ocean"}
Body = {"Normal"}
Eyes = {"Big", "Small", "Monolid"}
Color = {"Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"}
Hands = {"None", "Robot", "Spider", "Bear"}
Extra = {"Empty", "Sand", "Dust", "Graffiti", "Aloe"}

I want to print a list that contains all possible permutations of each element mentioned in the arrays above, following the order in which these arrays were set (i.e. it starts checking on Background, then goes to check Body, then Eyes, then Color, then Hands, and finishes on Extra).

The first permutation should be:

1. Ocean, Normal, Big, Yellow, None, Empty

The second permutation should be:

2. Ocean, Normal, Big, Yellow, None, Sand

And so on...

It can be assumed that the item None is the same as Empty.

How could I do that?

5
  • 1
    Does this help? stackoverflow.com/a/2535934/8739330 Commented Dec 19, 2021 at 7:52
  • @West This solution also works I think, but I don't know where in that line of code can I set a counter i, also, do you know if there's a formula I could use to check if the final number of i is really the total number of permutations for this case? Commented Dec 19, 2021 at 8:03
  • 1
    Seen your comment on accepted answer. i in that case will definitely be the total number of permutations. For my suggested solution, you can just get the length of the resulting list and compare that to your final i in accepted answer. If they are the same, you can then just use the fastest option. Commented Dec 20, 2021 at 3:50
  • 1
    @West Thank you. Also, I just realized that by multiplying each number of elements in each array, you ended up with the total number of permutations, i.e. 1x1x3x6x4x5 = 360 Commented Dec 20, 2021 at 18:57
  • Oh nice, I never thought of that Commented Dec 21, 2021 at 0:41

1 Answer 1

1

The following solution should work Assume arrays are as follows

Backgrounds = ["Ocean"]
Bodys = ["Normal"]
Eyes = ["Big", "Small", "Monolid"]
Colors = ["Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"]
Hands = ["None", "Robot", "Spider", "Bear"]
Extra = ["Empty", "Sand", "Dust", "Graffiti", "Aloe"]
for background in Backgrounds:
    for body in Bodys:
       for eye in Eyes:
           for colour in colours:
                 for hand in Hands:
                      for extra in extras:
                             print(background,body,eye,colour,hand,extra)

If your lists are really large please don't use the above solution because its time complexity is o(n^6).

Sign up to request clarification or add additional context in comments.

2 Comments

Cool, I think it works, I also added i = 0 right before the first for loop and i +=1 right before print(i,background,body,eye,colour,hand,extra), the counter goes up to 360, is there a formula I can use to evaluate if this total number of permutations (i.e. 360) is actually the right one for this case?
i+=1 should be after the print statement. If you wish only the first 360 outputs please add a break statement inside for loop. if you want 360th output no need for forloops. Also, accept answer if it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.