I have a problem. How do I execute a number of if statements but also change the amount of dictionary indexes as well? I think my code sums it up pretty well what I want to happen, but I'll explain further. with dict = {"Hi":{"Hello":{"Greetings":"Goodbye"}}}
I want a set of if statements to be able to access every point in this dictionary without having to type each one individually. So for this one,
If level == 1:
print(dict["Hi"])
If level == 2:
print(dict["Hi"]["Hello"])
If level == 3:
print(dict["Hi"]["Hello"]["Greetings"])
An example piece of code:
E = {"C:":{"Desktop.fld":{"Hello.txt":{"Content":"Hello, World"}}}}
def PATH_APPEND(path, item, location, content):
if len(location) == 1:
E[location[0]] = item
E[location[0]][item] = content
if len(location) == 2:
E[location[0]][location[1]] = item
E[location[0]][location[1]][item] = content
if len(location) == 3:
E[location[0]][location[1]][location[2]][item] = content
# ... and so on
PATH_APPEND(E, "Hi.txt", ["C:","Desktop.fld"], "Hi There, World")
print(E)
#{"C:":{"Desktop.fld":{ ... , "Hi.txt":{"Content":"Hi There, World"}}}}
Aucun commentaire:
Enregistrer un commentaire