How to print each possible permutation in a dictionary that has arrays as values? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to print each possible permutation in a dictionary that has arrays as values? (/thread-35882.html) |
How to print each possible permutation in a dictionary that has arrays as values? - noahverner1995 - Dec-26-2021 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:. 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
RE: How to print each possible permutation in a dictionary that has arrays as values? - paul18fr - Dec-26-2021 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}') RE: How to print each possible permutation in a dictionary that has arrays as values? - noahverner1995 - Dec-27-2021 (Dec-26-2021, 12:37 PM)paul18fr Wrote: Maybe with: Thanks for your reply! I made a little improvement for my case, due to the need for grouping the elements in an specific order , 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}') |