How To Find A Separate Attribute Among The Records And Form A Separate Collection On It?
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,markg
Solution 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
Post a Comment for "How To Find A Separate Attribute Among The Records And Form A Separate Collection On It?"