(1)题目
(2)今天分析的解法
n = int(input())
scores = []
for i in range(n):
score = list(map(int,input().split())) #读取语、数、外成绩
scores.append([i+1,sum(score)] +score) #存储学号、总分、语、数、外成绩
index = reversed((1,2))
for i in index: #依次按总分和语文成绩排序
scores.sort(key=lambda x:x[i],reverse=True)
#依次对语文成绩,总分按降序排列,因为原有各子列表中学号已按升序排序,若总分与语文成绩均相同,则列表原有顺序不改变,即学号按升序排列
for i in range(5):
print(scores[i][0],scores[i][1])
知识点1:
index = reversed((1,2))语句的index并非是元组类型,它的类型是reversed,是一种迭代器若用作for循环,用起来等价于翻转后的元组
(可用print(type(index))来识别index的类型)
知识点2:
1.scores.append([i+1,sum(score)] +score)中 [i+1,sum(score)]并不是列表,而是将这个整体看做一个字符串,便于存储2.sum(score)说明sum函数可直接对列表内的数据求和
下检验是实现列表的部分求和
n = int(input())
scores = []
for i in range(n):
score = list(map(int,input().split()))
scores.append([i+1,sum(score[:2])] +score)
print(scores)
检验知可以
知识点3:
for i in index: scores.sort(key=lambda x:x[i],reverse=True) lambda是一个隐函数,是固定写法,不要写成别的单词,x表示列表中的一个元素,在这里,表示一个迭代器index,等价于一个元组