class Student:
def __init__(self,email,score,rank):
self.email=email
self.score=score
self.rank=rank
N,G,K=map(eval,input().split(" "))
student_list=[]
sum_score=0
while True:
a=list(map(eval,input().split()))
student_list.append(Student(a[0],a[1]))
if a=="":
break
if a[1]>G:
sum_score+=50
if G>=a[1]>=60:
sum_score+=20
print("{}".format(sum_score))
sorted(student_list,key=lambda x:(x.score,x.email))
student_list[0].rank=1
count=1
for i in range(1,N):
if(student_list[i].score!=student_list[i-1].score):
student_list[i].rank=student_list[i-1].rank+count
count=1
else:
student_list[i].rank=student_list[i-1].rank
count+=1
for i in range(K):
print("{} {} {}".format(student_list[i].rank,student_list[i].email,student_list[i].score))
存在输入问题
class Student:
def __init__(self,email,score,rank):
self.email=email
self.score=score
self.rank=rank
N,G,K=map(eval,input().split(" "))
student_list=[]
sum_score=0
W=N
while W!=0 :
W-=1
a=list(input().split(" "))
student_list.append(Student(a[0],a[1],rank=1))
if a=="":
break
if int(a[1])>=G:
sum_score+=50
if G>int(a[1])>=60:
sum_score+=20
print("{}".format(sum_score))
sorted(student_list,key=lambda x:(x.score,x.email))
student_list[0].rank=1
count=1
for i in range(1,N):
if(student_list[i].score!=student_list[i-1].score):
student_list[i].rank=student_list[i-1].rank+count
count=1
else:
student_list[i].rank=student_list[i-1].rank
count+=1
for i in range(K):
print("{} {} {}".format(student_list[i].rank,student_list[i].email,student_list[i].score))
sorted函数的问题
import copy
from operator import attrgetter
class Student:
def __init__(self,email,score,rank):
self.email=email
self.score=score
self.rank=rank
N,G,K=map(eval,input().split(" "))
student_list=[]
sum_score=0
W=copy.deepcopy(N)
while W!=0 :
W-=1
a=list(input().split(" "))
student_list.append(Student(a[0],a[1],rank=1))
if not a:
break
if int(a[1])>=G:
sum_score+=50
if G>int(a[1])>=60:
sum_score+=20
print("{}".format(sum_score))
sorted_student_list=sorted(student_list,key=attrgetter("score","email"),reverse=True)
sorted_student_list[0].rank=1
count=1
for i in range(1,N):
if(sorted_student_list[i].score!=sorted_student_list[i-1].score):
sorted_student_list[i].rank=sorted_student_list[i-1].rank+count
count=1
else:
sorted_student_list[i].rank=sorted_student_list[i-1].rank
count+=1
for i in sorted_student_list:
if i.rank <=K:
print("{} {} {}".format(i.rank,i.email,i.score))
搞了半天,原来语法没有错,逻辑错误:排序后的列表要用新的列表来进行存储。。。
然后又遇到了sorted函数的问题(查看官方文档)
import copy
from operator import attrgetter
def multisort(xs, specs):
for key, reverse in reversed(specs):
xs.sort(key=attrgetter(key), reverse=reverse)
return xs
class Student:
def __init__(self,email,score,rank):
self.email=email
self.score=score
self.rank=rank
N,G,K=map(eval,input().split(" "))
student_list=[]
sum_score=0
W=copy.deepcopy(N)
while W!=0 :
W-=1
a=list(input().split(" "))
student_list.append(Student(a[0],a[1],rank=1))
if not a:
break
if int(a[1])>=G:
sum_score+=50
if G>int(a[1])>=60:
sum_score+=20
print("{}".format(sum_score))
sorted_student_list=multisort(list(student_list),(('score', True), ('email', False)))
sorted_student_list[0].rank=1
count=1
for i in range(1,N):
if(sorted_student_list[i].score!=sorted_student_list[i-1].score):
sorted_student_list[i].rank=sorted_student_list[i-1].rank+count
count=1
else:
sorted_student_list[i].rank=sorted_student_list[i-1].rank
count+=1
for i in sorted_student_list:
if i.rank <=K:
print("{} {} {}".format(i.rank,i.email,i.score))
没到满分,有时间再看吧
总结:
如果你看到我这篇文章,你将掌握一个新的sort函数我将其称之为multisort,也就是官方文档的称呼,也就是说,拥有此函数,你将能解决n重排序的麻烦!!!!!!
这可以被抽象为一个包装函数,该函数能接受一个列表以及字段和顺序的元组,以对它们进行多重排序。
from operator import attrgetter
def multisort(xs, specs):
for key, reverse in reversed(specs):
xs.sort(key=attrgetter(key), reverse=reverse)
return xs
sorted_student_list=multisort(list(student_list),(('score', True), ('email', False)))