是否有另一种不同的方式在字典中打印笛卡尔积,其中数组作为值遵循 Python 上的特定顺序?
Is there another different way of printing a cartesian product in a dictionary which has arrays as values following an specific order on Python?
假设我有以下字典:
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']}
一种打印每个可能组合的方法是使用 itertools
,此处:
import itertools as it
AllKeysNames = the_dictionary_list.keys()
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
这将打印一个 list
,总共有 360
个元素。
但是,假设在前一种情况下顺序很重要(即需要排列),则此顺序如下:
'Fondo'>'Cuerpo'>'Ojos'>'Color'>'Pinzas'>'Puas'
我如何制作一个始终以“Fondo”键的值开头,然后添加“Cuerpo”值的程序'键,然后添加'Color'键的第一个值,然后添加'Pinzas'键的第一个值,然后添加第一个值'Puas' 键等等...?
最终新列表中的元素总数仍为 360
,但这次这些元素将按照特定顺序创建。
有什么想法吗?
我终于想通了,我什至决定让用户来决定排列顺序。
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
while True:
AllKeysNames = the_dictionary_list.keys()
print('3[46m'+str(AllKeysNames)+'3[0m')
ele = input("3[0;37;40mTell me which valid key you want me to set now:3[0m ")
if ele in the_dictionary_list and ele not in Keys_input:
Keys_input.append(ele) # adding the element
i += 1
print(f'3[0;37;42mThe array has been updated, its current storage is the following {Keys_input}3[0m')
if i == n:
print("\u001b[45mThe array is now full, let's continue with the next step3[0m")
break
else:
if ele not in the_dictionary_list:
print('\u001b[43mPlease, type only valid key names3[0m')
if ele in Keys_input:
print('\u001b[43mStop, that key IS ALREADY SAVED in the array, try with a different valid one3[0m')
print(f'\u001b[45mCurrent storage of the array is the following {Keys_input}3[0m')
AllKeysNames2 = Keys_input
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames2)))
print(f'{Combinations}')
假设我有以下字典:
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']}
一种打印每个可能组合的方法是使用 itertools
,此处:
import itertools as it
AllKeysNames = the_dictionary_list.keys()
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
这将打印一个 list
,总共有 360
个元素。
但是,假设在前一种情况下顺序很重要(即需要排列),则此顺序如下:
'Fondo'>'Cuerpo'>'Ojos'>'Color'>'Pinzas'>'Puas'
我如何制作一个始终以“Fondo”键的值开头,然后添加“Cuerpo”值的程序'键,然后添加'Color'键的第一个值,然后添加'Pinzas'键的第一个值,然后添加第一个值'Puas' 键等等...?
最终新列表中的元素总数仍为 360
,但这次这些元素将按照特定顺序创建。
有什么想法吗?
我终于想通了,我什至决定让用户来决定排列顺序。
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
while True:
AllKeysNames = the_dictionary_list.keys()
print('3[46m'+str(AllKeysNames)+'3[0m')
ele = input("3[0;37;40mTell me which valid key you want me to set now:3[0m ")
if ele in the_dictionary_list and ele not in Keys_input:
Keys_input.append(ele) # adding the element
i += 1
print(f'3[0;37;42mThe array has been updated, its current storage is the following {Keys_input}3[0m')
if i == n:
print("\u001b[45mThe array is now full, let's continue with the next step3[0m")
break
else:
if ele not in the_dictionary_list:
print('\u001b[43mPlease, type only valid key names3[0m')
if ele in Keys_input:
print('\u001b[43mStop, that key IS ALREADY SAVED in the array, try with a different valid one3[0m')
print(f'\u001b[45mCurrent storage of the array is the following {Keys_input}3[0m')
AllKeysNames2 = Keys_input
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames2)))
print(f'{Combinations}')