ratchet600
ratchet600

Reputation: 79

Why does Python(3.7) keep overriding key:value pairs in my nested dictionary?

I want to preface this by saying I replicated the spirit of the code in the python terminal by writing:

A = {}
A["Q"] = {}
A["Q"]["creation"] = {}
A["Q"]["creation"]["reactants_1"] = ["R1","R2"]

printing A gives what one would expect:

{"Q" : {"creation" : {"reactants_1" : ["R1","R2"]}}}

then adding:

A["Q"]["creation"]["reactants_2"] = ["R3","R4"]

yields:

{"Q" : {"creation" : {"reactants_1" : ["R1","R2"], "reactants_2" : ["R3","R4"]}}}

However I have a function displayed below which, when run correctly assigns the first few key:value pairs but when it loops over to the second value of x it replaces the first key:value pair it wrote earlier with a new one despite the key being different.

using the above example we would only get:

{"Q" : {"creation" : {"reactants_2" : ["R3","R4"]}}}

Reactions is an array containing things like "e+e+p=e+H_1" in the format:

["e+e+p=e+H_1", "e+e+p=e+H_2",...,"e+H_10=e+e+p"]

Species is an array like:

[["e", 100000],["p", 100000],...]

where for now we only care about the string part.

diff_input is an empty dictionary to begin with

reactions_const contains, for each reaction, the left and right sides separate - seen as [x][0][0] and [x][0][1] early in the function as well as some other information that is not important for now.

rates_names is an array of unique identifiers for each reaction i can use later hence using the dictionary approach.

the print statements are all me trying to figure out why it isn't working

def rate_eqns(diff_input, reactions, species, reactions_const, 
          rates_names):

for x in range(len(reactions)):
    # for each reaction
    # split the left and right hand side up into lists of their 
    # respective species involved for counting later

    print("reaction: " + str(x) + " " + str(reactions[x]))

    species_lhs = reactions_const[x][0][0].split('+')
    print("LHS = " + str(species_lhs))

    species_rhs = reactions_const[x][0][1].split('+')

    for y in range(len(species)):  
        # For each species, create a sub-dictionary
        diff_input[species[y][0]] = {}

        # create sub-dictionaries in each species for creation, destruction and preservation/neutral paths
        diff_input[species[y][0]]["destruction"] = {}
        diff_input[species[y][0]]["creation"] = {}
        diff_input[species[y][0]]["balanced"] = {}

        # check if species occurs in each reaction

        if species[y][0] in reactions[x][0]:

            # if you start with more of it than you finish its destruction
            if species_lhs.count(species[y][0]) > species_rhs.count(species[y][0]):

                # if true: add an entry to the dictionary which holds the reaction identifier
                # bound to the destruction/creation/balanced identifier bound to the species identifier.
                print("species:" + str(species[y][0]) + " found net loss from reactants")
                print("LHS = " + str(species_lhs))
                print("RHS = " + str(species_rhs))
                print("reaction designation = " + str(rates_names[x]) + " Destruction of species")
                print("-------------")
                diff_input[species[y][0]]["destruction"][rates_names[x]] = species_lhs

            elif species_lhs.count(species[y][0]) == species_rhs.count(species[y][0]):
                print("species:" + str(species[y][0]) + " found no change in number")
                print("LHS = " + str(species_lhs))
                print("RHS = " + str(species_rhs))
                print("reaction designation = " + str(rates_names[x]) + " preservation of species")
                diff_input[species[y][0]]["balanced"][rates_names[x]] = species_lhs

            elif species_lhs.count(species[y][0]) < species_rhs.count(species[y][0]):
                print("species:" + str(species[y][0]) + " found net gain from reactants")
                print("LHS = " + str(species_lhs))
                print("RHS = " + str(species_rhs))
                print("reaction designation = " + str(rates_names[x]) + " creation of species")
                diff_input[species[y][0]]["creation"][rates_names[x]] = species_lhs

        # else:
            # print(str(species[y][0]) + " not found in reaction")
        print(diff_input)
        a = input("press return to continue")
with open('diff_input.txt', 'w') as file:
    file.write(str(diff_input))

return diff_input

the file saving part is optional, has anyone else every encountered a dictionary overriding existing keys with new keys?

Thanks for your patience and I appreciate any advice on my formatting (I tried to make it as good as possible without including the rest of the script)

Upvotes: 1

Views: 543

Answers (1)

blhsing
blhsing

Reputation: 107134

The actual value of your species[1][0] must happen to be equal to species[0][0], so that when it loops over to the second value of x, the assignment diff_input[species[y][0]] = {} would overwrite the sub-dict of the previous iteration since species[y][0] stays the same.

To put it more simply using your example code, in your outer loop you are making the following initialization:

A["Q"] = {}
A["Q"]["creation"] = {}

so even if your inner loop assigns some values to the sub-dict:

A["Q"]["creation"]["reactants_1"] = ["R1","R2"]

A["Q"] = {} would overwrite it in the next iteration as long as "Q" continues to be the main key for the assignment.

Upvotes: 1

Related Questions