본문 바로가기

Language/Python

RePythonOOP 6일차 네임드 튜플 클래스 응용 해 보기

 

 

이름을 찾아줘..

네임드 튜플을 응용하여 학교의 반, 학생정보를 가지는 클래스를 선언 해보겠습니다. 

 

반마다 20명의 인원  , 4개의 ABCD반이 있고 

 A반 1~20명, B반 1~20명 ... 있는 구조입니다.

 

전체 코드입니다.

 

from collections import namedtuple
Classes = namedtuple('Classes', ['rank', 'number'])
numbers = [str(n) for n in range(1, 21)]
ranks = 'A B C D'.split()
students = [Classes(rank, number) for rank in ranks for number in numbers] 

print('len -', len(students))
print('students -', students)

students2 = [Classes(rank, number) 
                    for rank in 'A B C D'.split() 
                        for number in [str(n) 
                            for n in range(1,21)]]


print('len -', len(students2))
print('students -', students2)

for s in students:
    print('students -', s)

 

네임드 튜플, 그룹리스트를 선언합니다 

 

Classes = namedtuple('Classes', ['rank', 'number'])

numbers = [str(n) for n in range(1, 21)]
ranks = 'A B C D'.split()

 

List Comprehension in namedtuple # 첫번째 for문 # NestedLoop 되어 있는 for문을 작성합니다

반마다 1~20명이 동일하게 들어 가야 하니까요

 

students = [Classes(rank, number) for rank in ranks for number in numbers] 

 

List Comprehension 구조 = 값 할당 || for문 || 조건문 입니다.

 

리스트 컴프리핸션의 구조 출력의 구조가 너무 가독성이 낮습니다.

조금 수정해 볼가요?

 

print('len -', len(students)) # 80
print('students -', students) # students - [Classes(rank='A', number='1'), Classes(rank='A', number='2'), Classes(rank='A', number='3'), Classes(rank='A', number='4'), Classes(rank='A', number='5'), Classes(rank='A', number='6'), Classes(rank='A', number='7'), Classes(rank='A', number='8'), Classes(rank='A', number='9'), Classes(rank='A', number='10'), Classes(rank='A', number='11'), Classes(rank='A', number='12'), Classes(rank='A', number='13'),

 

가독성이 +1 증가하였습니다.

 

students2 = [Classes(rank, number) 
                    for rank in 'A B C D'.split() 
                        for number in [str(n) 
                            for n in range(1,21)]]

 

이번엔 출력을 고쳐보겠습니다.

print('student')는 들여쓰기를 해줘야 할거같군요

 

print('len-', len(students2)) len- 80
print('students -', students2) students - [Classes(rank='A', number='1'), Classes(rank='A', number='2'), Classes(rank='A', number='3'), Classes(rank='A', number='4'), Classes(rank='A', number='5'), Classes(rank='A', number='6'), Classes(rank='A', number='7'), Classes(rank='A', number='8'), Classes(rank='A', number='9'), Classes(rank='A', number='10'), Classes(rank='A', number='11'), Classes(rank='A', number='12'), Classes(rank='A', number='13'), Classes(rank='A', number='14'), Classes(rank='A', number='15'), Classes(rank='A', number='16'), Classes(rank='A', number='17'), Classes(rank='A', number='18'), Classes(rank='A', number='19'), Classes(rank='A', number='20'), Classes(rank='B', number='1'), Classes(rank='B', number='2'), Classes(rank='B', number='3'), Classes(rank='B', number='4'), Classes(rank='B', number='5'), Classes(rank='B', number='6'), Classes(rank='B', number='7'), Classes(rank='B', number='8'), Classes(rank='B', number='9'), Classes(rank='B', number='10'), Classes(rank='B', number='11'), Classes(rank='B', number='12'), Classes(rank='B', number='13'), Classes(rank='B', number='14'), Classes(rank='B', number='15'), Classes(rank='B', number='16'), Classes(rank='B', number='17'), Classes(rank='B', number='18'), Classes(rank='B', number='19'), Classes(rank='B', number='20'), Classes(rank='C', number='1'), Classes(rank='C', number='2'),

 

한결 깔끔해졌습니다.

 

for s in students:
    print('students -', s)
...
# students - Classes(rank='A', number='17')
# students - Classes(rank='A', number='18')
# students - Classes(rank='A', number='19')
# students - Classes(rank='A', number='20')
# students - Classes(rank='B', number='1')
# students - Classes(rank='B', number='2')
# students - Classes(rank='B', number='3')
# students - Classes(rank='B', number='4')
# students - Classes(rank='B', number='5')
# students - Classes(rank='B', number='6')
# students - Classes(rank='B', number='7')
# students - Classes(rank='B', number='8')
...