python - 在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 = ' ')
解决方案
您可以计算单词的长度,从单词之间所需的空格中减去它,然后在最后添加。
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
推荐阅读
- python - 是否需要在 Windows 10 中安装 Xyce 才能使用 PySpice?或者 PySpice 是否已经使用“Xyce 引擎”运行?
- php - PHP,将 iTunes RSS 转换为 JSON
- bash - 如何使用 GNU 并行处理 'read word1 word2' 的等价物
- vb.net - 如何保存已选中的复选框?
- javascript - “var opn = 要求(‘opn’);” 在 linux mint 上安装软件包时不起作用
- angular - 在 Angular 6 中创建新组件
- vb6 - 如何在 Visual Basic 6 中的窗体内创建静态成员?
- unity3d - 为什么我的 Unity 2.5D 纹理会根据相机 x 位置更改绘制顺序?
- reactjs - 如何使用 laravel-mix 在 componentDidMount() 中触发 .click() 等 DOM-api 方法
- java - 通过 Nutch Rest API 添加 URL 过滤器正则表达式