简体   繁体   English

从三个不同长度的单独列表创建嵌套字典

[英]Create nested dictionary from three separate lists with different length

I need to create one nested dictionary to describe six (6) students scores for 3 exams:我需要创建一个嵌套字典来描述六 (6) 名学生的 3 门考试成绩:

dd = {
    'T': {'Exam 1': 100, 'Exam 2': 90, 'Exam 3': 80},
    'M': {'Exam 1': 88, 'Exam 2': 99, 'Exam 3': 111},
    'F': {'Exam 1': 45, 'Exam 2': 56, 'Exam 3': 67},
    'R': {'Exam 1': 59, 'Exam 2': 61, 'Exam 3': 67},
    'U': {'Exam 1': 73, 'Exam 2': 79, 'Exam 3': 83},
    'F': {'Exam 1': 89, 'Exam 2': 97, 'Exam 3': 101}
}

Original input and prep are:原始输入和准备是:

import pandas as pd

grades = [
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['T', '100', '90', '80'],
    ['M', '88', '99', '111'],
    ['F', '45', '56', '67'],
    ['R', '59', '61', '67'],
    ['U', '73', '79', '83'],
    ['F', '89', '97', '101']
]

headers = grades.pop(0)
df = pd.DataFrame(grades, columns=headers)

df[['Exam 1','Exam 2','Exam 3']] = df[['Exam 1','Exam 2','Exam 3']].astype(int)

I can create the lists for dictionary creation:我可以为字典创建创建列表:

students = df['Student'].tolist()
assignments = list(df.columns.values)
assignments.remove('Student')

Problem:问题:

internal dictionary: keys - assignment (3), values - scores (6) Outer dictionary: keys - students (6), values - assignments (3)内部字典:键 - 作业 (3),值 - 分数 (6) 外部字典:键 - 学生 (6),值 - 作业 (3)

I do not know how to handle the scores list!!不知道成绩单怎么处理!! It is 6 per assignment but is it a list of lists (3 x 6)??每个作业是 6 个,但它是一个列表 (3 x 6) 吗??

I would like to use the zip function:我想使用 zip 功能:

keys = students
values = assignments

result = dict(zip(keys, values))

but I do not know how to iterate internal dictionary:但我不知道如何迭代内部字典:

keys = assignments. (3 elements)
values = scores (has to be 3 by 6) #-  how to iterate it if it is possible at all?

result = dict(zip(keys, values))

You can use pandas to_dict function directly on your dataframe.您可以直接在数据帧上使用 pandas to_dict函数。 However, your dataframe has duplicate students, so you won't be able to get your example dictionary exactly (must have unique keys).但是,您的数据框有重复的学生,因此您将无法准确获取示例字典(必须具有唯一键)。

Removing the duplicate "F" student for illustration...删除重复的“F”学生以进行说明......

import pandas as pd

grades = [

    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['T', '100', '90', '80'],
    ['M', '88', '99', '111'],
    ['F', '45', '56', '67'],
    ['R', '59', '61', '67'],
    ['U', '73', '79', '83']
]

headers = grades.pop(0)
df = pd.DataFrame(grades, columns=headers)

df[['Exam 1','Exam 2','Exam 3']] = df[['Exam 1','Exam 2','Exam 3']].astype(int)

df = df.set_index('Student')
dd = df.to_dict(orient='index')
print(dd)

{'T': {'Exam 1': 100, 'Exam 2': 90, 'Exam 3': 80}, 
'M': {'Exam 1': 88, 'Exam 2': 99, 'Exam 3': 111}, 
'F': {'Exam 1': 45, 'Exam 2': 56, 'Exam 3': 67}, 
'R': {'Exam 1': 59, 'Exam 2': 61, 'Exam 3': 67}, 
'U': {'Exam 1': 73, 'Exam 2': 79, 'Exam 3': 83}}

First, you have two students named "F" but dict keys have to be unique.首先,您有两个名为“F”的学生,但dict键必须是唯一的。 You could then try the following:然后您可以尝试以下操作:

grades = [
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['T', '100', '90', '80'],
    ['M', '88', '99', '111'],
    ['F', '45', '56', '67'],
    ['R', '59', '61', '67'],
    ['U', '73', '79', '83'],
    ['F', '89', '97', '101']
]

dd = {}
for name, exam1, exam2, exam3 in grades[1:]:
    dd[name] = {'Exam 1': exam1, 'Exam 2': exam2, 'Exam 3': exam3}

or:或者:

keys = [keys[0] for keys in grades[1:]]
values = [dict(zip(['Exam 1', 'Exam 2', 'Exam 3'], keys[1:])) for keys in grades[1:]]
dd = dict(zip(keys, values))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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