用split()函数,将字符串分成三个部分。先构造字典,再把字典添加入列表,然后用迭代循环取出字典。用value()函数将字典转化为列表,构造name,number,score三个列表,列表中元素一一对应,在score列表中使用max()和min()函数求出最大值和最小值,再中列表的index方法求出索引号,最后输出索引号对应的name和number。
student_list = []
n = int(input())
for i in range(0, n):
student_str = input()
student_str = student_str.split()
student_dict = {"name": student_str[0],
"number": student_str[1],
"score": int(student_str[2])}
student_list.append(student_dict)
name = []
number = []
score = []
for student in student_list:
result = list(student.values())
name.append(result[0])
number.append(result[1])
score.append((result[2]))
index = score.index(max(score))
print(name[index], number[index])
index = score.index(min(score))
print(name[index], number[index])
可以优化一下,不需要用字典
name = []
number = []
score =[]
n = int(input())
for i in range(n):
student_str = input()
student_str = student_str.split()
name.append(student_str[0])
number.append(student_str[1])
score.append(int(student_str[2]))
index = score.index(max(score))
print(name[index],number[index])
index = score.index(min(score))
print(name[index],number[index])
|