Home > database >  Creating nested subdict python
Creating nested subdict python

Time:03-11

I have some student names of different types and scores of each type in a list. Eg:

students_exam_names = [exam_name1, exam_name2, exam_name3]
students_exam_score = [exam_score1, exam_score2, exam_score3]
students_quiz_names = [quiz_name1, quiz_name2]
students_quiz_score = [quiz_score1, quiz_score2]
students_homework_names = [homework_name1, homework_name2, homework_name3, homework_name4]
students_homework_score = [homework_score1, homework_score2, homework_score3, homework_score4]

Similarly for all three as shown below.

I want to have the details in the form of nested dict as follows:

details = {'students_exam':{
    'exam_name1':exam_score1,
    'exam_name2':exam_score2,
    'exam_name3':exam_score3
},
'students_quiz':{
    'quiz_name1': quiz_score1,
    'quiz_name2': quiz_score2
},
'students_homework':{
    'homework_name1': homework_score1,
    'homework_name2': homework_score2,
    'homework_name3': homework_score3,
    'homework_name4': homework_score4,
}

The length of each students type is different. I tried to get it in the form of list of dictionaries as below but couldn't go further.

students_exam = {}

for i in range(len(students_exam_names)):
  students_exam[students_exam_names[i]] = students_exam_score[i]

CodePudding user response:

So what if i assume your complete set of inputs are like

students_exam_names = ['name1', 'name2', 'name3']
students_exam_score = ['score1', 'score2', 'score3']
students_quiz_names = ['name1', 'name2']
students_quiz_score = ['score1', 'score2']
students_homework_names = ['name1', 'name2', 'name3', 'name4']
students_homework_score = ['score1', 'score2', 'score3', 'score4'] 

if so then the following code should do the job.

details={}
details['students_exam']={sexam: students_exam_score[students_exam_names.index(sexam)] for sexam in students_exam_names}
details['students_quiz']={squiz: students_quiz_score[students_quiz_names.index(squiz)] for squiz in students_quiz_names}
details['students_homework']={shome: students_homework_score[students_homework_names.index(shome)] for shome in students_homework_names}

CodePudding user response:

It looks like you need some functions to do these updates:

def update_exam(details, names, scores):
    results = {}
    for name,score in zip(names,scores):
        results[name]=score
    details['students_exam'] = results

def update_quiz(details, names, scores):
    results = {}
    for name,score in zip(names,scores):
        results[name]=score
    details['students_quiz'] = results

def update_homework(details, names, scores):
    results = {}
    for name,score in zip(names,scores):
        results[name]=score
    details['students_homework'] = results

details = {}
update_exam(details, students_exam_names, students_exam_score)
update_quiz(details, students_quiz_names, students_quiz_score)
update_homework(details, students_homework_names, students_homework_score)

But since the above functions only really differ in the text name of the key, they can be collapsed further:

def update(details, key, names, scores):
    results = {}
    for name,score in zip(names,scores):
        results[name]=score
    details[key] = results

details = {}

update(details,'students_exam', students_exam_names, students_exam_score)
update(details,'students_quiz', students_quiz_names, students_quiz_score)
update(details,'students_homework', students_homework_names, students_homework_score)

And then the loop can become a dictionary comprehension:

def update(details, key, names, scores):
    details[key] = {name:score for (name,score) in zip(names,scores)}
  • Related