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']}
A way of printing each possible combination is by using itertools
, here:
import itertools as it
AllKeysNames = the_dictionary_list.keys()
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
This will print a list
which has 360
elements in total.
However, let's say that the order matters in the previous case (i.e. permutations are needed), this order would be the following:
'Fondo'>'Cuerpo'>'Ojos'>'Color'>'Pinzas'>'Puas'
How could I make a program that always start with the value of 'Fondo' key, then adds the value of 'Cuerpo' key, then adds the first value of 'Color' key, then adds the first value of 'Pinzas' key, then adds the first value of 'Puas' key and so on...?
In the end the total amount of elements in the new list would still be 360
, but this time these elements would have been created following an specific order.
Any ideas?
I finally figured it out, I even decided to let the user be the one who decides the order in which the permutations will be made.
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
while True:
AllKeysNames = the_dictionary_list.keys()
print('\033[46m'+str(AllKeysNames)+'\033[0m')
ele = input("\033[0;37;40mTell me which valid key you want me to set now:\033[0m ")
if ele in the_dictionary_list and ele not in Keys_input:
Keys_input.append(ele) # adding the element
i += 1
print(f'\033[0;37;42mThe array has been updated, its current storage is the following {Keys_input}\033[0m')
if i == n:
print("\u001b[45mThe array is now full, let's continue with the next step\033[0m")
break
else:
if ele not in the_dictionary_list:
print('\u001b[43mPlease, type only valid key names\033[0m')
if ele in Keys_input:
print('\u001b[43mStop, that key IS ALREADY SAVED in the array, try with a different valid one\033[0m')
print(f'\u001b[45mCurrent storage of the array is the following {Keys_input}\033[0m')
AllKeysNames2 = Keys_input
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames2)))
print(f'{Combinations}')