首页 > 解决方案 > 在python的for循环中将许多列表添加到嵌套字典中

问题描述

我的作业是我想创建五个列表并将这五个列表附加到字典中。例如:

student_names = ['Mike', 'Sarah', 'Matthew','Smith', 'Melanie', 'Renjith', 'Rahul', 'Mark', 
'Rebeka','Jenna']

students_math_marks = [91.99,94.43,89,85.23,90.31,88,78.6,92,94.35,80.72]

students_english_marks = [83.56,95.34,83,90.44,88.32,95,97.7,91,95.54,89.32]

students_physics_marks = [92.19,89.51,90,88.55,89.45,97,89.8,87,81.72,93.54]

students_chemistry_marks = [89.64,87.63,98,94.66,93.78,85,86.9,95,84.81,91.86]

问题1:拿一本字典。

示例字典结构应如下(示例):

student_info = {'Mike': {'math': 91.99, 'english': 83.56, 'physics': 92.19, 'chemistry': 89.64, 
'total_marks': 357.38}}

问题2:最终输出如下:

在此处输入图像描述

我的代码如下:

student_names = ['james','john','david','michael','richard','paul','mark','william','thomas','steven']
students_math_marks  = [90.11,89.23,99.99,93.33,92.11,85.31,93.90,93.88,92.82,89.09]
students_english_marks = [88.91,90.21,97.35,98.44,93.55,96.01,99.99,99.50,94.88,95.05]
students_physics_marks = [99.05,92.33,94.44,95.55,96.66,97.77,98.88,99.99,91.11,90.98]
students_chemistry_marks = [90.05,99,91.88,92.77,93.66,94.55,95.44,96.33,97.22,98.11,99.99] 

print('name     math        english        physics        chemistry      total marks ')

print('----------------------------------------------------------------------------------')

student_info = {} # professor ask us to create an empty dictionary.

for n,m,e,p,c in zip(student_names,students_math_marks,students_english_marks,students_physics_marks,students_chemistry_marks):
    sum=round((m+e+p+c),2)
    student_info = {n:{"math":float(m),'english':float(e),'physics':float(p),'chemistry':float(c),'total_marks':sum}}
    for key, value in student_info.items():
        print(f"\n{key}", end = '     ')
        for inkey, invalue in value.items():
            print(invalue, end = '         ')

我的输出如下: 在此处输入图像描述

标签: pythonpython-3.x

解决方案


您可以计算单词的长度,从单词之间所需的空格中减去它,然后在最后添加。

student_names = ['james','john','david','michael','richard','paul','mark','william','thomas','steven']
students_math_marks  = [90.11,89.23,99.99,93.33,92.11,85.31,93.90,93.88,92.82,89.09]
students_english_marks = [88.91,90.21,97.35,98.44,93.55,96.01,99.99,99.50,94.88,95.05]
students_physics_marks = [99.05,92.33,94.44,95.55,96.66,97.77,98.88,99.99,91.11,90.98]
students_chemistry_marks = [90.05,99,91.88,92.77,93.66,94.55,95.44,96.33,97.22,98.11,99.99] 

print('name           math           english        physics       chemistry      total marks ')

print('----------------------------------------------------------------------------------')

student_info = {} # professor ask us to create an empty dictionary.
spacek =0
spacev =0
for n,m,e,p,c in zip(student_names,students_math_marks,students_english_marks,students_physics_marks,students_chemistry_marks):
    sum=round((m+e+p+c),2)
    student_info = {n:{"math":float(m),'english':float(e),'physics':float(p),'chemistry':float(c),'total_marks':sum}}
    for key, value in student_info.items():
        spacek =15-len(key)
        print(f"\n{key}",end=spacek*" ")
        for inkey, invalue in value.items():
            spacev =15-len(str(invalue)) 
            print(invalue, end = spacev*" ")# your code goes here

推荐阅读