파이썬 팁

python 예제 - 등수매기기 - zip, operator.itemgetter(),np.arange, np.ramdom.randint()

gomming 2021. 2. 23. 00:27

 

등수대로 나열하고 특정 학생의 등수를 출력하도록 함.

import numpy as np
import operator

score_list = np.random.randint(0,100,100)
number = np.arange(100)+1

score_set = list (zip(number,score_list,))

print(score_set)

print ('================================')
print ('score_list = \n%s'%score_list)
print ('================================')
print ('number = \n%s'%number)
print ('================================')
rank = zip(number, score_list)
rank = sorted(rank, key=operator.itemgetter(1), reverse=True)
print('rank_list is \n%s'%rank)
print ('================================')

print (rank[:10])

target_student = 4
print (rank.index((score_set[target_student-1]))+1)

 

코드 설명

1) 1~100번의 번호를 갖는 학생 리스트를 만들고 (0~100점 사이의)임의의 점수를 부여함.

score_list = np.random.randint(0,100,100)

number = np.arange(100)+1

score_list =

[61 7 40 0 33 62 34 51 34 23 54 35 49 14 15 1 76 64 79 31 20 68 79 78 69 60 7 65 2 47 72 67 1 46 14 29 84 60 23 43 69 41 95 99 81 69 58 50 8 60 73 70 48 64 14 33 9 60 24 2 61 35 97 2 46 95 45 4 44 21 45 98

63 67 89 10 72 89 74 92 22 31 55 60 3 23 65 95 65 48 97 54 17 14 79 71 92 4 41 31]

================================

number =

[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

91 92 93 94 95 96 97 98 99 100]

2) 점수와 학생번호를 연결하여 묶어 튜플로 만듬

score_set = list (zip(number,score_list))

[(1, 61), (2, 7), (3, 40), (4, 0), (5, 33), (6, 62), (7, 34), (8, 51), (9, 34), (10, 23), (11, 54), (12, 35), (13, 49), (14, 14), (15, 15), (16, 1), (17, 76), (18, 64), (19, 79), (20, 31), (21, 20), (22, 68), (23, 79), (24, 78), (25, 69), (26, 60), (27, 7), (28, 65), (29, 2), (30, 47), (31, 72), (32, 67), (33, 1), (34, 46), (35, 14), (36, 29), (37, 84), (38, 60), (39, 23), (40, 43), (41, 69), (42, 41), (43, 95), (44, 99), (45, 81), (46, 69), (47, 58), (48, 50), (49, 8), (50, 60), (51, 73), (52, 70), (53, 48), (54, 64), (55, 14), (56, 33), (57, 9), (58, 60), (59, 24), (60, 2), (61, 61), (62, 35), (63, 97), (64, 2), (65, 46), (66, 95), (67, 45), (68, 4), (69, 44), (70, 21), (71, 45), (72, 98), (73, 63), (74, 67), (75, 89), (76, 10), (77, 72), (78, 89), (79, 74), (80, 92), (81, 22), (82, 31), (83, 55), (84, 60), (85, 3), (86, 23), (87, 65), (88, 95), (89, 65), (90, 48), (91, 97), (92, 54), (93, 17), (94, 14), (95, 79), (96, 71), (97, 92), (98, 4), (99, 41), (100, 31)]

3) 점수를 기준으로 내림차순으로 정렬된 튜플 리스트를 생성함.

rank = zip(number, score_list)

rank = sorted(rank, key=operator.itemgetter(1), reverse=True)

rank_list is

[(44, 99), (72, 98), (63, 97), (91, 97), (43, 95), (66, 95), (88, 95), (80, 92), (97, 92), (75, 89), (78, 89), (37, 84), (45, 81), (19, 79), (23, 79), (95, 79), (24, 78), (17, 76), (79, 74), (51, 73), (31, 72), (77, 72), (96, 71), (52, 70), (25, 69), (41, 69), (46, 69), (22, 68), (32, 67), (74, 67), (28, 65), (87, 65), (89, 65), (18, 64), (54, 64), (73, 63), (6, 62), (1, 61), (61, 61), (26, 60), (38, 60), (50, 60), (58, 60), (84, 60), (47, 58), (83, 55), (11, 54), (92, 54), (8, 51), (48, 50), (13, 49), (53, 48), (90, 48), (30, 47), (34, 46), (65, 46), (67, 45), (71, 45), (69, 44), (40, 43), (42, 41), (99, 41), (3, 40), (12, 35), (62, 35), (7, 34), (9, 34), (5, 33), (56, 33), (20, 31), (82, 31), (100, 31), (36, 29), (59, 24), (10, 23), (39, 23), (86, 23), (81, 22), (70, 21), (21, 20), (93, 17), (15, 15), (14, 14), (35, 14), (55, 14), (94, 14), (76, 10), (57, 9), (49, 8), (2, 7), (27, 7), (68, 4), (98, 4), (85, 3), (29, 2), (60, 2), (64, 2), (16, 1), (33, 1), (4, 0)]

4) 특정 번호 학생의 등수를 출력함.

target_student = 4

print (rank.index((score_set[target_student-1]))+1)

100

4번학생은 0점으로 100등으로 밝혀짐ㄷ

근데 공동 1등이 나오면 어떻하지?