一、模仿C语言的结构体读入数据
class student:
def __init__(self):
self.name = ""
self.num = ""
self.age = ""
list1 = []
for i in range(0, 3):
list1.append(student())
list1[i].name, list1[i].num, list1[i].age = input().split()
list1[i].age = int(list1[i].age)
二、结构体自定义排序cmp实现
n = int(input())
class student:
def __init__(self):
self.name = ""
self.num = ""
self.sco = 0
from functools import cmp_to_key
def cmp(a, b):
if a.sco < b.sco:
return -1
else:
return 1
list1 = []
for i in range(0, n):
list1.append(student())
list1[i].name, list1[i].num, list1[i].sco = input().split()
list1[i].sco = int(list1[i].sco)
list1.sort(key=cmp_to_key(cmp))
print(list1[0].name, list1[0].num, list1[0].sco)
三、结构体自定义排序lambda实现
n = int(input())
class student:
def __init__(self):
self.name = ""
self.num = ""
self.sco = 0
list1 = []
for i in range(0, n):
list1.append(student())
list1[i].name, list1[i].num, list1[i].sco = input().split()
list1[i].sco = int(list1[i].sco)
list1.sort(key=lambda x: x.sco)
print(list1[0].name, list1[0].num, list1[0].sco)