我怎样才能改进这个程序,让它在最后一个元素是使用 Python 剩下的最后一个元素时自动将最后一个元素添加到数组中?
How can I improve this program to make it add the last element to the array automatically when such element is the last one left using Python?
以下程序让用户定义将 the_dictionary_list
中的键名(元素)插入到 Keys_input
中的顺序:
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']}
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
print('\n')
print('The following "keys" represent the name of the folders in the current path')
while True:
AllKeysNames = the_dictionary_list.keys()
print('3[46m'+str(AllKeysNames)+'3[0m')
ele = input("3[0;37;40mNow It's time to define the order in which the Cartesian Products will be made, tell 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')
现在,假设用户为 Keys_input
中的元素选择以下顺序:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
如何改进上面的程序,以便在没有更多键名时自动将剩下的最后一个元素(在本例中为 'Puas'
)添加到数组中?我的意思是,在 Keys_input
数组更新如下后:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas']
想通了。将倒数第二个元素添加到数组后,程序将使用 for 循环遍历字典,并评估哪个键不在数组中,即不存在的那个(因为它永远是最后一个)将由程序自动添加。在此之后,程序打破了原始代码
的while True
循环
if i == (n-1):
for key in the_dictionary_list:
if key not in Keys_input:
Keys_input.append(key)
print(f'3[0;37;42mLet me add the last key, the final storage is the following {Keys_input}3[0m')
print("\u001b[45mThe array is now full, let's continue with the next step3[0m")
break
以下程序让用户定义将 the_dictionary_list
中的键名(元素)插入到 Keys_input
中的顺序:
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']}
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
print('\n')
print('The following "keys" represent the name of the folders in the current path')
while True:
AllKeysNames = the_dictionary_list.keys()
print('3[46m'+str(AllKeysNames)+'3[0m')
ele = input("3[0;37;40mNow It's time to define the order in which the Cartesian Products will be made, tell 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')
现在,假设用户为 Keys_input
中的元素选择以下顺序:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
如何改进上面的程序,以便在没有更多键名时自动将剩下的最后一个元素(在本例中为 'Puas'
)添加到数组中?我的意思是,在 Keys_input
数组更新如下后:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas']
想通了。将倒数第二个元素添加到数组后,程序将使用 for 循环遍历字典,并评估哪个键不在数组中,即不存在的那个(因为它永远是最后一个)将由程序自动添加。在此之后,程序打破了原始代码
的while True
循环
if i == (n-1):
for key in the_dictionary_list:
if key not in Keys_input:
Keys_input.append(key)
print(f'3[0;37;42mLet me add the last key, the final storage is the following {Keys_input}3[0m')
print("\u001b[45mThe array is now full, let's continue with the next step3[0m")
break