简体   繁体   中英

Creating a nested dictionary with data from pre-existing nested dictionary

I need to create a dictionary whose keys are unique course codes and whose values are dictionaries with a single key-value pair describing the sections available for that course code, in alphabetical order, as well as the ID numbers that belong to the specific section, in ascending order.

The data is from a dictionary called student_data that looks like this:

student_data[:3]

>>>[{'enlistment': [{'course code': 'UK 60', 'section': 'A'}, 
{'course code': 'GF 24', 'section': 'B'}, 
{'course code': 'ME 40', 'section': 'B'}, 
{'course code': 'VY 44', 'section': 'D'}, 
{'course code': 'EN 94', 'section': 'B'}], 
'id': '201001', 'paid': True, 'school': 'SOSE', 'year level': 2}, 
{'enlistment': [{'course code': 'EQ 61', 'section': 'D'}, 
{'course code': 'UZ 22', 'section': 'B'}, 
{'course code': 'KS 36', 'section': 'B'}, 
{'course code': 'VH 63', 'section': 'A'}, 
{'course code': 'IW 81', 'section': 'C'}], 
'id': '211002', 'paid': True, 'school': 'JGSOM', 'year level': 1}, 
{'enlistment': [{'course code': 'WE 15', 'section': 'D'}, 
{'course code': 'ZP 68', 'section': 'A'}, 
{'course code': 'GI 78', 'section': 'A'}, 
{'course code': 'GK 72', 'section': 'C'}, 
{'course code': 'FA 24', 'section': 'D'}, 
{'course code': 'UJ 28', 'section': 'A'}], 
'id': '201003', 'paid': True, 'school': 'JGSOM', 'year level': 2}]

Format of the desired output:

{
    course_code: [{
        "section": section_letter,
        "class list": [
           id_number_1,
           id_number_2,
           id_number_3,
        ]
    }
    course_code: [{
        "section": section_letter,
        "class list": [
           id_number_1,
           id_number_2,
           id_number_3,
        ]
    }
]
}

I'm really not good with dictionaries in Python because I don't understand it that much yet. Please help.

You can use collections.defaultdict :

from collections import defaultdict
d = defaultdict(dict)
student_data = [{'enlistment': [{'course code': 'UK 60', 'section': 'A'}, {'course code': 'GF 24', 'section': 'B'}, {'course code': 'ME 40', 'section': 'B'}, {'course code': 'VY 44', 'section': 'D'}, {'course code': 'EN 94', 'section': 'B'}], 'id': '201001', 'paid': True, 'school': 'SOSE', 'year level': 2}, {'enlistment': [{'course code': 'EQ 61', 'section': 'D'}, {'course code': 'UZ 22', 'section': 'B'}, {'course code': 'KS 36', 'section': 'B'}, {'course code': 'VH 63', 'section': 'A'}, {'course code': 'IW 81', 'section': 'C'}], 'id': '211002', 'paid': True, 'school': 'JGSOM', 'year level': 1}, {'enlistment': [{'course code': 'WE 15', 'section': 'D'}, {'course code': 'ZP 68', 'section': 'A'}, {'course code': 'GI 78', 'section': 'A'}, {'course code': 'GK 72', 'section': 'C'}, {'course code': 'FA 24', 'section': 'D'}, {'course code': 'UJ 28', 'section': 'A'}], 'id': '201003', 'paid': True, 'school': 'JGSOM', 'year level': 2}]
for i in student_data:
   for c in i['enlistment']:
      if c['section'] in d[c['course code']]:
          d[c['course code']][c['section']].append(i['id'])
      else:
          d[c['course code']][c['section']] = [i['id']]

r = {a:[{'section':c, 'class list':d} for c, d in b.items()] for a, b in d.items()}

Output:

{'UK 60': [{'section': 'A', 'class list': ['201001']}], 'GF 24': [{'section': 'B', 'class list': ['201001']}], 'ME 40': [{'section': 'B', 'class list': ['201001']}], 'VY 44': [{'section': 'D', 'class list': ['201001']}], 'EN 94': [{'section': 'B', 'class list': ['201001']}], 'EQ 61': [{'section': 'D', 'class list': ['211002']}], 'UZ 22': [{'section': 'B', 'class list': ['211002']}], 'KS 36': [{'section': 'B', 'class list': ['211002']}], 'VH 63': [{'section': 'A', 'class list': ['211002']}], 'IW 81': [{'section': 'C', 'class list': ['211002']}], 'WE 15': [{'section': 'D', 'class list': ['201003']}], 'ZP 68': [{'section': 'A', 'class list': ['201003']}], 'GI 78': [{'section': 'A', 'class list': ['201003']}], 'GK 72': [{'section': 'C', 'class list': ['201003']}], 'FA 24': [{'section': 'D', 'class list': ['201003']}], 'UJ 28': [{'section': 'A', 'class list': ['201003']}]}

Okay, Let me teach you what dictionaries in python are. They are unordered set of data having syntax of {key:value, key:value} All keys needed to be different from each other. In other words, Key has to be unique. Say, you have a dictionary, d = {1:"XYZ", 2:"WER"} 1 and 2 are keys where as "XYZ" and "WER" are respective values

so, if you want to get data of any key, just type, d[key] say, d[1]. You'll get "XYZ" as answer If you don't know the keys, just type d.keys() . It will print all the keys and then, using the loop

for i in d.keys():
        print(d[i])

This will give you all the values even if you don't know the keys...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM