π μ΄ ν¬μ€ν μμλ Pythonμ NamedTupleμ μ¬μ©νλ μ΄μ μ μ¬μ© λ°©λ²μ λν΄ μμλ³΄κ² μ΅λλ€.
π₯ namedtupleμ μ μΈκΉμ?
π₯ namedtuple μ΄λ»κ² μ¬μ©ν κΉ?
π₯ namedtupleμ 맀μλ μ 리
π₯ namedtuple νμ© μμ
βοΈ λ€μλννμ κ°μ²΄μ μΈμ€ν΄μ€λ₯Ό μμ±ν λ―μ΄ ννμ μμ±νμ¬ κ° μμμ μ΄λ¦μΌλ‘ μ κ·Όμ΄ κ°λ₯ν ννμ μλ―Έν©λλ€.
βοΈ μ¦, λ°μ΄ν°μ κ°μ μμ ν νμκ° μλ€λ©΄ ννμ κ°μ μ΄λ¦μΌλ‘ μ κ·Όν μ μκΈ° λλ¬Έμ μΌλ° ννμ΄ μΈλ±μ€λ‘ μ κ·Όνλ κ²μ λΉν΄ μ§κ΄μ μ
λλ€.
βοΈ λν λ€μλννμ μΌλ° κ°μ²΄ ννλ³΄λ€ μ μ λ©λͺ¨λ¦¬λ₯Ό μ¬μ©νκ³ , λ€μν μ κ·Όλ²μ μ§μν©λλ€.
βοΈ λ©λͺ¨λ¦¬ ν¨μ¨μ± μΌλ°μ μΈ μν©μμ 리μ€νΈλ³΄λ€ ννμ΄ μ’κ³ , ννλ³΄λ€ λ€μλννμ΄ μ’μ΅λλ€.
βοΈ λ μ μ¬μ΄μ 거리λ₯Ό ꡬνλ 곡μμ μλ‘ λ€μ΄, μΌλ°ννκ³Ό λ€μλννμ μ°¨μ΄λ₯Ό μμλ³΄κ² μ΅λλ€. λ μ μ¬μ΄μ 거리λ μλ 곡μμ μ¬μ©νμ¬ κ΅¬ν μ μμ΅λλ€.
βοΈ μΌλ° ννμ μΈλ±μ€ λ²νΈλ‘ κ°μ μ κ·Όν μ μκΈ° λλ¬Έμ κ°μ μ§κ΄μ μΌλ‘ μ΄ν΄νκΈ° μ΄λ €μ κ°μ μΈμ°κ³ μμ΄μΌνκ±°λ, μ£Όμμ΄ νμν©λλ€.
from math import sqrt point1 = (1.0, 5.0) # x1=1.0, y1=5.0 point2 = (2.5, 1.5) # x2=2.5, y2=1.5 line_len = sqrt((point2[0]-point1[0]) ** 2 + (point2[1]-point1[1]) ** 2) print(line_len) # 3.8078865529319543
βοΈ μ΄μ λ°ν΄, namedtupleμ μ¬μ©νλ©΄ κ° λ³μκ° κ°λ₯΄ν€λ κ°μ λ³΄λ€ μ§κ΄μ μΌλ‘ μ κ·Όνκ³ νμ©ν μ μμ΅λλ€.
βοΈ namedtupleμ μ΄λ¦μμ μ μ μλ―μ΄ λ€μλννμ μ΄λ¦μ κ°μ§ tupleμ μλ―ΈνκΈ° λλ¬Έμ tupleμ κ° μμμ λ μ΄λΈμ μ€ μ μμ΅λλ€.
βοΈ λ€μλννμ λ€μν λ°μ΄ν° νμ
μ μ§μνλ collections λ΄μ μ‘΄μ¬νκ³ , κ°μ²΄μ²λΌ μμ±νμ¬ μ¬μ©ν μ μμ΅λλ€.
from math import sqrt from collections import namedtuple # π "namedtuple" import Point = namedtuple('Point', 'x y') # π namedtuple μ μΈ point1 = Point(1.0, 5.0) point2 = Point(2.5, 1.5) line_len = sqrt((point2.x-point1.x) ** 2 + (point2.y-point1.y) ** 2) print(line_len) # 3.8078865529319543
βοΈ λ¦¬μ€νΈ ννλ‘ λ³μ μ μΈ : π Point1 = namedtuple('Point', ['x', 'y'])
βοΈ μ½€λ§ μ½μ
μΌλ‘ μ μΈ : π Point2 = namedtuple('Point', 'x, y')
βοΈ λμ΄μ°κΈ° μ½μ
μΌλ‘ μ μΈ : π Point3 = namedtuple('Point', 'x y')
βοΈ rename μ΅μ
μ¬μ© : π Point4 = namedtuple('Point', 'x y x class', rename=True)
# λ€μλ νν μ μΈ λ°©λ² Point1 = namedtuple('Point',['x', 'y']) Point2 = namedtuple('Point', 'x, y') Point3 = namedtuple('Point', 'x y') Point4 = namedtuple('Point', 'x y x class', rename=True) # defaultλ Falseμ print(Point1, Point2, Point3, Point4) # <class '__main__.Point'> <class '__main__.Point'> <class '__main__.Point'> <class '__main__.Point'>
βοΈ namedTuple λν tupleμ²λΌ λΆλ³μ νΉμ±μ μ§λκ³ μκΈ° λλ¬Έμ μ€λ³΅λ μ μκ³ , λΉμ°ν "class"μ κ°μ μμ½μ΄ λ€μ μΈμ€ν΄μ€ λ³μμ²λΌ μ§μ ν μ μμ΅λλ€.
βοΈ λ°λΌμ μλμ κ°μ codeλ xκ°μ΄ μ€λ³΅λμ΄ μκ³ , classλΌλ μμ½μ΄κ° μ μΈ μ μ¬μ©λμκΈ° λλ¬Έμ μλ¬λ₯Ό λ°μμν΅λλ€.
Point4 = namedtuple('Point', 'x y x class')
π xμ€λ³΅, class μμ½μ΄βοΈ μ΄λ΄ κ²½μ°, rename μ΅μ
μ Trueλ‘ νμ±ννλ©΄ μ€λ³΅λλ κ° λλ μμ½μ΄ λ±μ μλͺ»λ λ³μλ‘ μ μΈ μ μ¬μ©ν΄λ μ€λ₯λ₯Ό λ°ννμ§ μκ³ , λλ€ν λ³μμ λ°μ΄ν°λ₯Ό ν λΉν΄μ€λλ€.
βοΈ λΏλ§μλλΌ λλΈμμ€ν(**)λ₯Ό μ¬μ©ν΄ λμ
λ리μ ννλ μΈν©νΉ ν΄μ€λλ€.
# λ€μλ νν μ μΈ λ°©λ² Point1 = namedtuple('Point',['x', 'y']) Point2 = namedtuple('Point', 'x, y') Point3 = namedtuple('Point', 'x y') Point4 = namedtuple('Point', 'x y x class', rename=True) # π defaultλ Falseμ λλ€. p1 = Point1(x=10, y=35) print(p1) # Point(x=10, y=35) p2 = Point2(20, 40) print(p2) # Point(x=20, y=40) p3 = Point3(45, y=20) print(p3) # Point(x=45, y=20) p4 = Point4(10, 20, 30, 40) print(p4) # Point(x=10, y=20, _2=30, _3=40) π μλμΌλ‘ renameν΄μ€λλ€:) # λμ λ리 μΈν©νΉ temp_dict = {'x':75, 'y':55} p5 = Point1(**temp_dict) # π λμ λ리 μΈν©νΉ print(p5) # Point(x=75, y=55) # unpacking νμ© x, y = p3 print(x+y) # 65
Point1 = namedtuple('Point', ['x', 'y']) temp = [52, 38] p4 = Point1._make(temp) print(p4) # Point(x=52, y=38)
Point1 = namedtuple('Point', ['x', 'y']) Point2 = namedtuple('Point', 'x, y') Point3 = namedtuple('Point', 'x y') print(p1._fields, p2._fields, p3._fields,) # ('x', 'y') ('x', 'y') ('x', 'y')
Point1 = namedtuple('Point', ['x', 'y']) p1 = Point1(x=10, y=35) temp = [52, 38] p4 = Point1._make(temp) print(p1._asdict(), p4._asdict()) # {'x': 10, 'y': 35} {'x': 52, 'y': 38} print(type(p1._asdict()), type(p4._asdict())) # <class 'dict'> <class 'dict'>
Point2 = namedtuple('Point', 'x, y') p2 = Point2(20, 40) print(p2) # Point(x=20, y=40) print(p2._replace(y=100)) # Point(x=20, y=100)
βοΈ 4κ°μ λ°(A,B,C,D)μ κ° 20λͺ
μ νμμ΄ μμ λ νμ μ 체 κ·Έλ£Ή μμ±
βοΈ 1λ²λΆν° 20λ²κΉμ§ νμ μμ± : π numbers = [str(n) for n in range(1,21)]
βοΈ 4κ°μ λ° μμ± : π ranks = 'A B C D'.split()
# 4κ°μ λ°μ A,B,C,Dμ κ° 20λͺ μ νμμ΄ μμ λ νμ μ 체 κ·Έλ£Ή μμ± from collections import namedtuple numbers = [str(n) for n in range(1,21)] # ['1', '2', '3', '4', ...... '17', '18', '19', '20'] ranks = 'A B C D'.split() # ['A', 'B', 'C', 'D'] Classes = namedtuple('Classes', ['rank', 'number']) # κ°μ²΄λͺ (classes)κ³Ό λ³μλͺ (classes)μ μΌμΉμν€λκ² κ΄λ‘μ students = [Classes(rank, number) for rank in ranks for number in numbers] print(len(students)) # 80 print(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'), Classes(rank='C', number='3'), Classes(rank='C', number='4'), Classes(rank='C', number='5'), Classes(rank='C', number='6'), Classes(rank='C', number='7'), Classes(rank='C', number='8'), Classes(rank='C', number='9'), Classes(rank='C', number='10'), Classes(rank='C', number='11'), Classes(rank='C', number='12'), Classes(rank='C', number='13'), Classes(rank='C', number='14'), Classes(rank='C', number='15'), Classes(rank='C', number='16'), Classes(rank='C', number='17'), Classes(rank='C', number='18'), Classes(rank='C', number='19'), Classes(rank='C', number='20'), Classes(rank='D', number='1'), Classes(rank='D', number='2'), Classes(rank='D', number='3'), Classes(rank='D', number='4'), Classes(rank='D', number='5'), Classes(rank='D', number='6'), Classes(rank='D', number='7'), Classes(rank='D', number='8'), Classes(rank='D', number='9'), Classes(rank='D', number='10'), Classes(rank='D', number='11'), Classes(rank='D', number='12'), Classes(rank='D', number='13'), Classes(rank='D', number='14'), Classes(rank='D', number='15'), Classes(rank='D', number='16'), Classes(rank='D', number='17'), Classes(rank='D', number='18'), Classes(rank='D', number='19'), Classes(rank='D', number='20')] """