【发布时间】: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))
输入数据
我的名声很小,我无法从我的计算机中添加图片,我发送了链接,我的输入数据在哪里,我在类中添加了什么以及我的对象类集合如何在控制台中显示。和组别
如何使平均成绩显示在每组学生列表之后的底部? 还是只计算每组的平均分,然后输入另一个字典?
我弄清楚了字典中值的排序方式。
我尝试制作第二本词典并将组放在那里,然后在单独的评估单元中计算它 - 它没有用。 然后我读到,在 Python 中,您可以将字典添加到字典中。 结果,我试图将现有字典添加到新字典中,但不明白如何进行。 因为无论如何都必须计算成绩
您不能使用任何辅助类,只能使用一个类。
【问题讨论】:
-
stdn,grds,srdbl in v.items()不正确。 v.items() 会给你[(k1, v1), (k2,v2), (k3,v3)....so on]。所以你可以解压2个变量(k,v)
标签: python python-3.x