Dec-16-2017, 06:15 AM
Let's go back to your initial code and initial output
Let's slightly reorganize the code by first populating innerdict, then assign it to the outerdict:
outerdict[key] = innerdict innerdict["number"] = line_tuple[0] innerdict["grade"] = line_tuple[2] innerdict["total"] = line_tuple[3] innerdict["weight"] = line_tuple[4]From the output, we see that we have the right data of the third record, three times. This is "normal" because, as I said, the keys of outerdict point to innerdict and not its hard values.
Let's slightly reorganize the code by first populating innerdict, then assign it to the outerdict:
innerdict["number"] = line_tuple[0] innerdict["grade"] = line_tuple[2] innerdict["total"] = line_tuple[3] innerdict["weight"] = line_tuple[4] outerdict[key] = innerdictAnd finally, how to tell python to copy the hard values of innerdict? You can use the following way:
outerdict[key] = dict(innerdict)(Note: to be fully correct, this statement is OK for making a copy of a "simple" dictionary which itself doesn't contain any complex objects like a list or a dictionary. For such cases, do a research on "copy" and "deepcopy")