what want code sum parcial grades of each student , return final grade, ordering them grade, and if students same grade, display high number student first.
my code:
nice={} try: open('grades.txt') file: data = file.readlines() except ioerror ioerr: print(str(ioerr)) each_line in data: (number, grade) = each_line.split() if number not in nice: nice[number]=0 nice[number]+=int(grade) in sorted(nice, key=lambda n: (-nice[n],n)): print(i,nice[i]) my input: file: "grades.txt"
10885 10 70000 6 70000 10 60000 4 70000 4 60000 4 10885 10 60001 5 60002 8 60003 8 my output:
10885 20 70000 20 60000 8 60002 8 60003 8 60001 5 as can see, i've found way sum each parcial grade final grade , display them grades, yet i've not figured out how implement other condition, getting desired output:
10885 20 70000 20 60003 8 60002 8 60000 8 60001 5 since students 60000,60002,60003 have same grade, should displayed above, 60003>60002>60000.
its helpful try , think of different (ie condition inverted)
sorted(nice.items(), key=lambda itm: map(int,itm[::-1]),reverse=true) or in python 3
sorted(nice.items(), key=lambda v: (int(v[1]),int(v[0])),reverse=true) or
sorted(nice.items(), key=lambda itm: list(map(int,itm[::-1])),reverse=true) should work python2 or 3
python3.4.3
>>> nice {'60000': '8', '60001': '5', '70000': '20', '60003': '8', '10885': '20', '60002': '8'} >>> sorted(nice.items(), key=lambda itm: list(map(int,itm[::-1])),reverse=true) [('70000', '20'), ('10885', '20'), ('60003', '8'), ('60002', '8'), ('60000', '8'), ('60001', '5')]
Comments
Post a Comment