DOCS : https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple
- 인덱스로 요소를 접근하는 튜플보다 Naming으로 직관적으로 접근이 가능. (마치 사전처럼)
- 변경 불가능한 자료형(immutable)으로 요소를 변경하지 않을 경우 사전보다 성능에 이점이 있다.
namedtuple (네임드 튜플)
import collections
Score = collections.namedtuple('Score', 'english math music ')
Student1 = Score(english=90, math=80, music=85)
print(Student1.english, Student1[0]) # name와 index 둘다 접근이 가능
print(Student1.math, Student1[1])
print(Student1.music, Student1[2])
90 90
80 80
85 85
사전(Dict)
Score = {'english':90, 'math':80, 'music':85}
print(Score['english'])
print(Score['math'])
print(Score['music'])
90
80
85
'Python > Data Struc & algo' 카테고리의 다른 글
[Python] IIS log date Sqlite3 Memory DB and pandas DataFrame (0) | 2018.05.24 |
---|---|
[Python] collections Counter를 이용한 합집합, 교집합, 차집합 구하기 (0) | 2018.05.13 |
[Python] collections deque를 이용하여 리스트의 왼쪽에 요소 추가하기 (0) | 2018.05.13 |
[Python] To find the sum of odd numbers up to the number range of N. (파이썬 N의 수 범위까지의 홀수의 합 구하기) (0) | 2018.04.22 |
[Python] 홀수의 정수 리스트에서 중간값 추출하기 (1) | 2018.04.19 |
[Python] 회문 판별(palindrome) (0) | 2018.04.18 |
프로그래머스 Lv2 - 자연수를 뒤집어 리스트로 만들기 (0) | 2018.04.18 |
프로그래머스 Lv1 - x만큼 간격이 있는 n개의 숫자 (0) | 2018.04.18 |