lst = []
while True:
student_dict = {}
no = input('学号:')
student_dict['学号'] = no
lst.append(student_dict)
choice = input('继续吗?(y/n)')
if choice.lower() in ['y', 'yes']:
continue
else:
break
sorted_lst = sorted(lst, key=lambda x: x['学号']) # 按学号升序排序
print(sorted_lst)
对lst
进行排序:lst.sort(key=lambda x : x["学号"])
,完整代码如下:
lst = []
while True:
dict = {}
no = input("学号:")
dict["学号"] = no
lst.append(dict)
choice = input("继续吗:")
if choice in ['Y', 'y']:
continue
else:
break
#增加排序
lst.sort(key=lambda x : x["学号"])
print(lst)