import random
number = random.sample(range(1, 45), 44)
grade = []
for i in range(45):
grade.append(int(random.normalvariate(70, 10)))
stu_dict = {key: value for key, value in zip(number, grade)}
print(stu_dict)
sort_stu = sorted(stu_dict.items())
print(sort_stu)
创建两个列表用zip连接,再创建一个字典一一个列表作为键一个作为值,最后用sorted函数排列
运行结果
{4: 65, 11: 72, 33: 66, 40: 73, 21: 75, 44: 45, 7: 71, 22: 79, 41: 67, 23: 60, 10: 73, 13: 67, 2: 83, 43: 59, 37: 64, 27: 85, 31: 60, 16: 68, 12: 73, 14: 53, 6: 85, 1: 61, 25: 70, 17: 63, 9: 57, 38: 70, 18: 60, 19: 68, 30: 64, 32: 60, 34: 70, 42: 64, 20: 72, 15: 53, 35: 49, 5: 65, 29: 78, 28: 70, 3: 82, 26: 77, 39: 56, 24: 69, 8: 79, 36: 51} [(1, 61), (2, 83), (3, 82), (4, 65), (5, 65), (6, 85), (7, 71), (8, 79), (9, 57), (10, 73), (11, 72), (12, 73), (13, 67), (14, 53), (15, 53), (16, 68), (17, 63), (18, 60), (19, 68), (20, 72), (21, 75), (22, 79), (23, 60), (24, 69), (25, 70), (26, 77), (27, 85), (28, 70), (29, 78), (30, 64), (31, 60), (32, 60), (33, 66), (34, 70), (35, 49), (36, 51), (37, 64), (38, 70), (39, 56), (40, 73), (41, 67), (42, 64), (43, 59), (44, 45)]
|