Ok so, the question may sound confusing at first but I'm gonna do my best to explain what I would like to learn in order to improve my programming skills.
Let's say I have a path in which exists 6 folders with the following file images:
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:
Pinzitas.png Pinzotas.png Pinzota_pinzita.png
Puas:
Arena.png Marron.png Purpura.png Verde.png
Now, I want the above information to be stored in a dictionary for further use, so I run the code below in the same path in which the folders previously mentioned exist:
import os
# Main method
the_dictionary_list = {}
for name in os.listdir("."):
if os.path.isdir(name):
path = os.path.basename(name)
print(f'\u001b[45m{path}\033[0m')
list_of_file_contents = os.listdir(path)
print(f'\033[46m{list_of_file_contents}')
the_dictionary_list[path] = list_of_file_contents
print('\n')
print('\u001b[43mthe_dictionary_list:\033[0m')
print(the_dictionary_list)
So after compiling the program above I get my dictionary:
But here's the problem: After creating the dictionary, how can I let the user decide in which arrays to add a 'None' string as a new value (i.e. not replacing the current ones), meaning that, for instance, if the user wanted to add 'None' only to Puas
Array and Pinzas
Array, it would generate the following output?:
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']}
CodePudding user response:
From what I can interpret, you would like to get the user input and insert None
to the front of each of those keys. As @robbo mentions, you can use insert. A text implementation of this would look like:
to_add = []
user_input = input("Which directory to add None to: ")
# Will exit when user gives null input
while user_input:
if user_input in the_dictionary_list:
to_add.append(user_input)
else:
print("Try again.")
user_input = input("Which directory to add None to: ")
# Add to dictionary
for key in to_add:
the_dictionary_list[key].insert(0, None)