(python) student records

[Problem description]
Read the student information of no more than 50 students from the keyboard (including names, student numbers, and age information separated by spaces, sorted by student number from low to high) [Input form] Each time the keyboard
is
read Student information of no more than 50 students:
the first line is the number of students;
each subsequent line is the student number, name, and age separated by spaces, where the student number and age are integers.
[Output format]
Output the student information in order of name (from low to high) and age (from low to high), and output the information of one student per line, in which the student number occupies 3 digits, and the name (English) occupies 6 digits, age occupies 3 digits, all are right-aligned. Sort by name from lowest to highest when the age is the same. The output results of the two sequences are separated by a blank line.
【Input sample】

4
1 aaa 22
45 bbb 23
54 ddd 20
110 ccc 19

【Example of output】     

  1   aaa 22        
 45   bbb 23      
110   ccc 19 
 54   ddd 20 
                                     
110   ccc 19        
 54   ddd 20         
  1   aaa 22        
 45   bbb 23

【Example description】

Enter four student records from the keyboard, sort and output them by name and age respectively.
[Scoring Standard]
Output student information in order of name and age.

 

n = int(input())
#n人数
my_list = []
#创建一个主列表
for i in range(n):
    j = []#创建新列表并添加到主序列表中
    my_list.append(j)#将n个列表添加到主列表里面
    for i in range(1):
        a,b,c=map(str,input().split(' '))
        j.append(a)
        j.append(b)
        j.append(c)
my_list2=sorted(my_list,key=(lambda x:x[1]))#姓名排序排序
for i in range(n):
     print("%3s%6s%3s"%(my_list2[i][0],my_list2[i][1],my_list2[i][2]))
print("\n")
my_list3=sorted(my_list,key=(lambda x:x[2]))#年龄排序排序
for i in range(n):
    if n>=2:
        if my_list3[0][2] == my_list3[1][2]:
            print("%3s%6s%3s"%(my_list2[i][0],my_list2[i][1],my_list2[i][2])) 
        else:
            print("%3s%6s%3s"%(my_list3[i][0],my_list3[i][1],my_list3[i][2]))
    else:
        print("%3s%6s%3s"%(my_list2[i][0],my_list2[i][1],my_list2[i][2]))

Description: Change the previous program

Guess you like

Origin blog.csdn.net/qq_62315940/article/details/127994523