Python Forum
How to print each possible permutation in a dictionary that has arrays as values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print each possible permutation in a dictionary that has arrays as values?
#1
Question 
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 = ["None", "Sand", "Dust", "Graffiti", "Aloe"]
I want to print a list that contains all possible permutations of each element mentioned 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).

So I use the following code:

i=0
for background in Background:
    for body in Body:
        for eye in Eyes:
           for colour in Color:
               for hand in Hands:
                     for extra in Extra:
                         i += 1
                         print(i,background,body,eye,colour,hand,extra)
Output:

Quote:.
.
.
358 Ocean Normal Monolid Orange Bear Dust
359 Ocean Normal Monolid Orange Bear Graffiti
360 Ocean Normal Monolid Orange Bear Aloe

Cool, but now let's say I have the following dictionary:

the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
How could I generate a similar output but now for this dictionary without having to use many for loops nor manually declare each one of them?

Btw: The total number of possible permutations is calculated by multiplying the lengths of each array, which for this case gives 360 permutations Wink
Reply
#2
Maybe with:

import itertools as it

the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 
                       'Cuerpo': ['Cuerpo_cangrejo.png'], 
                       'Fondo': ['Oceano.png'], 
                       'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 
                       'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 
                       'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}


AllKeysNames = the_dictionary_list.keys()
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
noahverner1995 likes this post
Reply
#3
(Dec-26-2021, 12:37 PM)paul18fr Wrote: Maybe with:

import itertools as it

the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 
                       'Cuerpo': ['Cuerpo_cangrejo.png'], 
                       'Fondo': ['Oceano.png'], 
                       'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 
                       'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 
                       'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}


AllKeysNames = the_dictionary_list.keys()
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')

Thanks for your reply!

I made a little improvement for my case, due to the need for grouping the elements in an specific order Smile , here:



import itertools as it
the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 
                       'Cuerpo': ['Cuerpo_cangrejo.png'], 
                       'Fondo': ['Oceano.png'], 
                       'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 
                       'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 
                       'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
 
AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 2 32 21 minutes ago
Last Post: deanhystad
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Print names in x-axis of a time-series values hobbyist 4 1,218 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Printing specific values out from a dictionary mcoliver88 6 1,383 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Getting values from a dictionary brunolelli 5 3,570 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,002 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,967 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Even/Odd permutation braankoo 9 11,605 Jan-10-2021, 01:19 AM
Last Post: Larz60+
  Adding keys and values to a dictionary giladal 3 2,475 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 1,926 Oct-29-2020, 12:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020