【问题标题】:How to calculate the marks for each group and deduce the average score under each group,如何计算每组的分数并推导出每组的平均分,
【发布时间】:2021-01-23 10:25:13
【问题描述】:

更新的问题和代码!30.01.2021

这个问题顺理成章地出现在这个问题之后 - How to find a separate attribute among the records and form a separate collection on it?

我需要计算每组的平均分。 在用户@itprorh66 建议的代码中,我添加了一个计算平均分的公式(它有效)和一个分数的条件搜索运算符。

@itprorh66 先生
我决定稍微修改您的代码以尝试自己解决我的问题。但到目前为止没有成功。

此代码块正在运行

def srtgr(self):
          counthrees=0
          countfours=0
          countfives=0
          midl_average=0      
          gl=dict()    #Dictionary for the group      
          for s in studinfos:
               sd=gl.pop(s.numbgroup,dict())
               sd[s.surn]=[s.markgeometry,s.markalgebra,s.markinformatika]
               gl[s.numbgroup]=sd
          for k,v, in gl.items():                  
                    s=f'Group: {k}: \n'
                    for stdn,grds in v.items():                  
                          s+=f'  {stdn}\t{grds[0]},{grds[1]},{grds[2]} \n'
                    print(s)

这是def srtgr 的第二部分。在此代码块中,我尝试计算每组中的成绩数(3,4 和 5),然后找到每组的平均成绩。在循环的帮助下,我开始遍历字典并在分支运算符的帮助下开始计算每个组的分数,但这就是我在编写这段代码时的想法。实际上,什么都没有开始,也不算数

for grds in gl.items():
                 if grds.markgeometry==3:
                      counthrees+=1
                 if grds.markalgebra==3:
                      counthrees+=1
                 if grds.markinformatika==3:
                      counthrees+=1
                 if grds.markgeometry==4:
                      countfours+=1
                 if grds.markalgebra==4:
                      countfours+=1
                 if grds.markinformatika==4:
                      countfours+=1
                 if grds.markgeometry==5:
                      countfives+=1
                 if grds.markalgebra==5:
                      countfives+=1
                 if grds.markinformatika==5:
                      countfives+=1
                 midl_average = (counthrees+countfours+countfives)/3
                 for kk,grds1 in gl.items():                  
                    grds=f'Group: {kk}: {grds1}\n'
                    print(grds)
          print(*sorted(gl, key = midl_average, reverse = True))

输入数据

我的名声很小,我无法从我的计算机中添加图片,我发送了链接,我的输入数据在哪里,我在类中添加了什么以及我的对象类集合如何在控制台中显示。和组别

https://ibb.co/zV1B0HC

如何使平均成绩显示在每组学生列表之后的底部? 还是只计算每组的平均分,然后输入另一个字典?

我弄清楚了字典中值的排序方式。

我尝试制作第二本词典并将组放在那里,然后在单独的评估单元中计算它 - 它没有用。 然后我读到,在 Python 中,您可以将字典添加到字典中。 结果,我试图将现有字典添加到新字典中,但不明白如何进行。 因为无论如何都必须计算成绩

您不能使用任何辅助类,只能使用一个类。

【问题讨论】:

  • stdn,grds,srdbl in v.items() 不正确。 v.items() 会给你[(k1, v1), (k2,v2), (k3,v3)....so on]。所以你可以解压2个变量(k,v)

标签: python python-3.x


【解决方案1】:

dict.items() 返回字典中的键值对。根据代码v字典有两种数据

  1. key 是学生编号,value 是成绩列表。
  2. key 是一个学生 numbgroup,value 是一个包含空列表的列表。

我最好的猜测是你不想要那本字典中的第二种类型的东西。我不知道红球的东西应该做什么,但目前你把它设为一个空列表,把它放在一个带有完全不同类型信息的字典中,然后用一个浮点数覆盖变量,而不会弄乱前面的空列表。

顺便说一句,不要害怕使用冗长的变量名。当我们需要计算出 k 和 v 是什么时,更难遵循代码。顺便说一句,我们将您包括在内,即使使用您自己的代码,错误的命名也会使诊断错误变得更加困难。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多