L2-3 名人堂与代金券

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)))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值