Python Forum
Looping to Create Nested Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping to Create Nested Dictionary
#1
Hello!

My code is supposed to read lines from a file that contains several attributes: assignment number, assignment name, grade, total, and weight. Each line of the file has this format.

My code reads the lines and turns it into a nested dictionary. Currently, the code seems to be looping and overwriting the key values of the inner dictionary.

Here is the file it's reading from:
1 assignment_1 85 100 0.25
2 test_1 90 100 0.25
3 exam_1 95 100 0.5
The code should return a nested dictionary with the assignment name as the key, and the inner dictionary having the keys "number", "grade", "total", and "weight" for each line.
The correct output:

Output:
{'assignment_1': {'total': 100, 'number': 1, 'grade': 85, 'weight': 0.25}, 'test_1': {'total': 100, 'number': 2, 'grade': 90, 'weight': 0.25}, 'exam_1': {'total': 100, 'number': 3, 'grade': 95, 'weight': 0.5}}
Here is my code:
def reader(filename):
    file_reader = open(filename)
    results = []
    innerdict = {}
    outerdict = {}

    for line in file_reader:
        parts = line.split(" ")
        line_tuple = (int(parts[0]), parts[1], int(parts[2]), int(parts[3]), float(parts[4]))
        key = line_tuple[1]
        outerdict[key] = innerdict 

        innerdict["number"] = line_tuple[0]
        innerdict["grade"] = line_tuple[2]
        innerdict["total"] = line_tuple[3]
        innerdict["weight"] = line_tuple[4]
        
         

    return outerdict
It returns this output:
Output:
{'assignment_1': {'number': 3, 'weight': 0.5, 'total': 100, 'grade': 95}, 'test_1': {'number': 3, 'weight': 0.5, 'total': 100, 'grade': 95}, 'exam_1': {'number': 3, 'weight': 0.5, 'total': 100, 'grade': 95}}
I believe I am somewhat close, I just do not know how to preserve the values of the inner dictionary once it loops over a line.

Thank you for your help! I've been trying to figure this out for a few days and just wish to understand.
Reply


Messages In This Thread
Looping to Create Nested Dictionary - by gngu2691 - Dec-15-2017, 10:29 PM
RE: Looping to Create Nested Dictionary - by wavic - Dec-15-2017, 11:04 PM
RE: Looping to Create Nested Dictionary - by wavic - Dec-16-2017, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  create empty sets for keys in a dictionary and add values to the set naughtysensei 1 2,529 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE
  how can i create a dictionary of dictionaries from a file Astone 2 2,279 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  nested looping with list cap510 2 1,928 Sep-10-2020, 04:51 AM
Last Post: cap510
  nested dictionary Maxime 3 3,607 Mar-14-2019, 07:19 AM
Last Post: perfringo
  Looping through a dictionary for every other value fad3r 15 9,548 Jan-25-2018, 07:12 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020