问题
There is a class that contains the following information The name of the student, his group, grades in geometry, algebra and computer science
def __init__(self,surn,numbgroup,markgeometry,markalgebra,markinformatika):
self.surn=surn
self.numbgroup=numbgroup
self.markgeometry=markgeometry
self.markalgebra=markalgebra
self.markinformatika=markinformatika
There are class objects, they are stored in the list - studinfos []
(Surname, Group, grades in 3 subjects ())
Smith ARP11 3,3,3
Brown ARP12 4,5,3
Jones ARP12 4,4,5
Johnson ARP13 4,5,4
Jensen ARP13 3,3,3
Williams ARP13 5,5,5
Adams ARP11 5,5,5
Wilson ARP12 5,4,5
Taylor ARP11 3,5,3
Anderson ARP11 5,3,5
The problem is that I need to calculate the GPA from this collection for each group of these students. That is, from this collection I need to form separate collections by groups and separately calculate their average score. For example, like this:
Smith ARP11 3,3,3
Adams ARP11 5,5,5
Taylor ARP11 3,5,3
Anderson ARP11 5,3,5
Brown ARP12 4,5,3
Jones ARP12 4,4,5
Wilson ARP12 5,4,5
Johnson ARP13 4,5,4
Jensen ARP13 3,3,3
Williams ARP13 5,5,5
How to calculate the average score in general, without dividing groups, I know how.
But I cannot figure out how to divide them into separate lists into groups (ARP11, ARP12, ARP13) and work separately with each one. Moreover, there can be more than 3 groups.
What is already in stock:
A list that contains a tuple to store. True, the values in the tuple are immutable like, and here the question is how to fill it.
listgroup=[(student.numbgroup, 0)]
Function that is not fully implemented (it remains only to figure out the groups).
Regarding the huge amount of if, please do not criticize, it correctly counts the number of evaluations. The midlbal variable itself is also working and tested. The sorted function separately also works.
def sortgroups(self):
listgroup=[(student.numbgroup, 0)]
countfour=0
countfive=0
counthree=0
for student in studinfos:
if student.markgeometry==4:
countfour+=1
if student.markalgebra==4:
countfour+=1
if student.markinformatika==4:
countfour+=1
if student.markgeometry==5:
countfive+=1
if student.markalgebra==5:
countfive+=1
if student.markinformatika==5:
countfive+=1
if student.markgeometry==3:
counthree+=1
if student.markalgebra==3:
counthree+=1
if student.markinformatika==3:
counthree+=1
for student in studinfos.numbofgroup[i]:
listgroup.append(get_numgroup)
midbal=((countfive+countfour+counthree)/3)
listgroup=[get_numbgroup(),midbal]
回答1:
I think from your description, this is what you are trying to do.
given:
class Student:
def __init__(self,surn,numbgroup,markgeometry,markalgebra,markinformatika):
self.surn=surn
self.numbgroup=numbgroup
self.markgeometry=markgeometry
self.markalgebra=markalgebra
self.markinformatika=markinformatika
A list of students:
Smith ARP11 3,3,3
Brown ARP12 4,5,3
Jones ARP12 4,4,5
Johnson ARP13 4,5,4
Jensen ARP13 3,3,3
Williams ARP13 5,5,5
Adams ARP11 5,5,5
Wilson ARP12 5,4,5
Taylor ARP11 3,5,3
Anderson ARP11 5,3,5
and a list studentinfos containing an instantiation of Student for each of the previous defined students, organize the data based on the class attribute numbgroup.
This is how I would do that:
gl = dict()
for s in studentinfos:
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 std, grds in v.items():
s += f' {std}\t{grds[0]}, {grds[1]}, {grds[2]}\n'
print (s)
Which produces:
Group: ARP13
Johnson 4, 5, 4
Jensen 3, 3, 3
Williams 5, 5, 5
Group: ARP12
Brown 4, 5, 3
Jones 4, 4, 5
Wilson 5, 4, 5
Group: ARP11
Smith 3, 3, 3
Adams 5, 5, 5
Taylor 3, 5, 3
Anderson 5, 3, 5
来源:https://stackoverflow.com/questions/65844300/how-to-find-a-separate-attribute-among-the-records-and-form-a-separate-collectio