[Python] Part II - Chap 3. Dictionaries and Sets

KwanHongΒ·2022λ…„ 11μ›” 19일
1

πŸ‘‰ 이 글은 <μ „λ¬Έκ°€λ₯Ό μœ„ν•œ 파이썬(fluent python) - λ£¨μ‹œμ•„λˆ„ ν•˜λ§λ₯˜> λ₯Ό λ²ˆμ—­ν–ˆμŠ΅λ‹ˆλ‹€.

λ“€μ–΄κ°€λ©° πŸ’­

Python을 쑰금 더 μœ μ°½ν•˜κ²Œ μ‚¬μš©ν•  λ•Œ πŸŽ’

νŒŒμ΄μ¬μ€ κ°œλ°œμžκ°€ 읽기도, μ΄ν•΄ν•˜κΈ°λ„ μ‰¬μ›Œλ³΄μ—¬ μ–΄λŠ 정도 μ΅μˆ™ν•΄μ‘Œλ‹€ 싢은 μˆœκ°„ ν•œ 단계 더 κΉŠμ€ 곡뢀가 ν•„μš”ν•΄μ§€λŠ” 언어라고 μƒκ°ν•œλ‹€. νŒŒμ΄μ¬μ΄λΌλŠ” μ–Έμ–΄κ°€ 가진 λ‚΄μž¬μ  퍼포먼슀의 ν•œκ³„λŠ” λ³„κ°œλ‘œ μΉ˜λ”λΌλ„, 파이썬이 μ‚¬λžŒμ˜ 언어와 λΉ„μŠ·ν•˜κ²Œ λ§Œλ“€κΈ° μœ„ν•΄ 잘 포μž₯ν•΄ 놓은 κ·Έ μ•ˆμ„ 쑰금 더 λ“€μ—¬λ‹€ λ³Ό ν•„μš”κ°€ μƒκΈ°λŠ” μˆœκ°„μ„ λ§ˆμ£Όν•˜κ²Œ λœλ‹€. 쑰금 더 μœ μ°½ν•œ 파이썬 μ–Έμ–΄ μ‚¬μš©μžκ°€ 되기 μœ„ν•œ 지식듀을 ν•˜λ‚˜μ”© μ†Œν™”μ‹œμΌœλ³΄λ©°, 이전에 무심코 μž‘μ„±ν•˜κ±°λ‚˜ μ°Έκ³ ν–ˆλ˜ μ½”λ“œλ“€μ„ κΉŠμ€ μˆ˜μ€€μ—μ„œ λ‹€μ‹œ 이해할 수 있게 λœλ‹€.

이 μ±•ν„°μ˜ λͺ©ν‘œλŠ” λ¬΄μ—‡μΈκ°€μš”? πŸ”Ž

  • Pythonμ΄λΌλŠ” μ–Έμ–΄λ₯Ό κ΅¬μ„±ν•˜λŠ” 핡심 μš”μ†ŒμΈ 'dict' νƒ€μž…μ— λŒ€ν•œ 이해도λ₯Ό 높인닀
  • 'dict' νƒ€μž…μ˜ 높은 μ„±λŠ₯을 보μž₯ν•˜λŠ” 'Hash table'을 μ•Œμ•„λ³Έλ‹€
  • 'Hash table'에 κΈ°λ°˜μ„ λ‘” λ‹€λ₯Έ νƒ€μž…μΈ 'set'을 μ΄ν•΄ν•œλ‹€

Chapter 3. Dictionaries and Sets

dict νƒ€μž…μ€ ν”„λ‘œκ·Έλž¨μ—μ„œ 널리 μ‚¬μš©λ  뿐만 μ•„λ‹ˆλΌ, 파이썬 κ΅¬ν˜„μ˜ 핡심 μš”μ†Œμ΄λ‹€. λͺ¨λ“ˆ λ„€μž„μŠ€νŽ˜μ΄μŠ€(Module namespaces), ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€ 속성, ν•¨μˆ˜ ν‚€μ›Œλ“œ 인자(Function keyword arguments)λŠ” dictionariesκ°€ 적용된 파이썬의 핡심 ꡬ쑰듀 쀑 일뢀닀.

dictλŠ” 핡심적인 역할을 ν•˜κΈ° λ•Œλ¬Έμ— 높은 μˆ˜μ€€μœΌλ‘œ μ΅œμ ν™” λ˜μ–΄μžˆλ‹€. 파이썬의 dictκ°€ 높은 μ„±λŠ₯을 κ°€μ§€λŠ” μ΄μœ λŠ” Hash tables에 κΈ°λ°˜μ„ 두기 λ•Œλ¬Έμ΄λ‹€.

이번 μ±•ν„°μ—μ„œ dict와 λ§ˆμ°¬κ°€μ§€λ‘œ hash tables둜 κ΅¬ν˜„λœ set νƒ€μž…λ„ 닀룬닀. dictionaries와 setsλ₯Ό κ΅¬ν˜„ν•  λ•Œ, Hash tables의 λ™μž‘ 방식을 μ•„λŠ” 것이 μ€‘μš”ν•˜λ‹€.


Generic Mapping Types

collections.abc λͺ¨λ“ˆμ€ dictλ‚˜ μœ μ‚¬ν•œ νƒ€μž…μ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν˜•νƒœν™”ν•˜λŠ” ABC인 Mappingκ³Ό MutableMapping을 μ œκ³΅ν•œλ‹€.

ν‘œμ€€ 라이브러리의 λͺ¨λ“  mapping types은 κΈ°λ³Έ dictλ₯Ό κ΅¬ν˜„μ— μ‚¬μš©ν•œλ‹€. λ”°λΌμ„œ, ν‚€(key)κ°€ hashable ν•΄μ•Ό ν•œλ‹€λŠ” ν•œκ³„μ μ€ λ™μΌν•˜λ‹€.

  • What is hashable?

    • 생애주기 λ™μ•ˆ λ°”λ€Œμ§€ μ•Šμ€ ν•΄μ‹œ 값을 가진 객체λ₯Ό hashableν•˜λ‹€κ³  ν•œλ‹€. (__hash__ λ©”μ†Œλ“œ ν•„μš”)
    • λ‹€λ₯Έ 객체와 비ꡐ할 수 μžˆμ–΄μ•Ό ν•œλ‹€. (__eq__ λ©”μ†Œλ“œ ν•„μš”)
    • κ°€μž₯ κΈ°λ³Έ λ‹¨μœ„(atomic)의 immutable type인 str, bytes, numeric typesλŠ” λͺ¨λ‘ hashable
    • frozenset λ˜ν•œ hashable (frozenset의 μ •μ˜μ— 따라 μ›μ†Œλ“€μ΄ hashable)
    • tupleλŠ” λͺ¨λ“  μ•„μ΄ν…œμ΄ hashable일 κ²½μš°μ—λ§Œ hashable
  • tuple의 hashability

tt = (1, 2, (30, 40))
hash(tt)  # 8027212646858338501
tl = (1, 2, [30, 40])  # TypeError: unhashable type: 'list'
tf = (1, 2, frozenset([30, 40]))
hash(tf)  # -4118419923444501110

이와 같은 κΈ°λ³Έ 원칙에 따라, dictionariesλ₯Ό μ—¬λŸ¬ 가지 λ°©λ²•μœΌλ‘œ λ§Œλ“€ 수 μžˆλ‹€.

  • Several ways of building dictionaries
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))  # zip returns iterator of tuples
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
a == b == c == d == e  # True

dict Comprehensions

파이썬 2.7λΆ€ν„° listcomps와 genexps이 dict comprehension에 μ μš©λ˜μ—ˆλ‹€. dictcompλŠ” μž„μ˜μ˜ iterableλ‘œλΆ€ν„° key:value 상을 μƒμ„±ν•˜μ—¬ dict 객체λ₯Ό λ§Œλ“ λ‹€.

  • λ™μΌν•œ tuple listλ‘œλΆ€ν„° 두 개의 dictionariesλ₯Ό λ§Œλ“œλŠ” dict comprehension μ‚¬μš©μ„ 보여쀀닀.
DIAL_CODES = [  # <1>
    (86, 'China'),
    (91, 'India'),
    (1, 'United States'),
    (62, 'Indonesia'),
    (55, 'Brazil'),
    (92, 'Pakistan'),
    (880, 'Bangladesh'),
    (234, 'Nigeria'),
    (7, 'Russia'),
    (81, 'Japan')
]
country_code = {country: code for code, country in DIAL_CODES}  # <2>
country_code  # {'China': 86, 'India': 91, 'Bangladesh': 880...} 
{code: country.upper() for country, code in country_code.items() if code < 66}  # {1: 'UNITED STATES', 55: 'BRAZIL', 62: 'INDONESIA', 7: 'RUSSIA'}

<1> μŒμ„ 가진 λ¦¬μŠ€νŠΈλŠ” dict μƒμ„±μžμ— λ°”λ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.
<2> 쌍의 μˆœμ„œκ°€ λ°”λ€Œμ—ˆλ‹€.

profile
λ³Έμ§ˆμ— μ§‘μ€‘ν•˜λ €κ³  λ…Έλ ₯ν•©λ‹ˆλ‹€. πŸ”¨

0개의 λŒ“κΈ€