๐ ์ด ํฌ์คํ ์์๋ 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')] """