์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/23
[python ์ฌํ] 6. ์๋ฃํ (์ง๋ฅํ ๋ฆฌ์คํธ, ์ ๋๋ ์ดํฐ, ๊ฐ๋ณ๋ถ๋ณ, ์ ๋ ฌ)
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/22 https://silvercoding.tistory.com/21 https://silvercoding.tistory.com/20 https://silvercoding.tistory.com/19..
silvercoding.tistory.com
- ํด์ํ ์ด๋ธ (hashtable)
ํด์ํ ์ด๋ธ์ ์์ ๋ฆฌ์์ค๋ก ๋ง์ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๋ค.
ํด์ ํ ์ด๋ธ์ key์ value๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค.
*** Hash๊ฐ์ ํ์ธ ํจ์ผ๋ก์จ ์ค๋ณต์ด ํ์ฉ๋๋์ง ์๋๋์ง ์ ์ ์์ .
# Hash ๊ฐ ํ์ธ ---> ์ค๋ณต์ด ํ์ฉ๋๋์ง ์๋๋์ง ,
t1 = (10, 20, (30, 40, 50))
t2 = (10, 20, [30, 40, 50])
print('EX1-2 - ', hash(t1))
# print('EX1-3 - ', hash(t2)) #---> ๋ฆฌ์คํธ๊ฐ ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์ ํด์ฌ ์ธ ์ ์์
- ์ง๋ฅํ ๋์ ๋๋ฆฌ (Comprehending Dict)
(์์ ) ๊ตญ๊ฐ์ฝ๋ csv ํ์ผ -> tuple ๋ณํ -> Dictionary ๋ณํ
(1) csv ํ์ผ ๋ถ๋ฌ์์ tuple๋ก ๋ณํํ๊ธฐ ( ํ์ผ: ํจ์บ ์ ๊ณต )
import csv
from os import kill
from typing import Counter
# ์ธ๋ถ CSV TO List of tuple
with open('./resources/test1.csv', 'r', encoding='UTF-8') as f:
temp = csv.reader(f)
# Header Skip
next(temp)
print()
# ๋ณํ
NA_CODES = [tuple(x) for x in temp]
print('EX2-1 - ')
print(NA_CODES)
๋์ค๊น์ง๋ง ์บก์ณ. ์ต์ข ์ ์ผ๋ก ๋๋ผ ์ฝ๋ ํํ์ด ๋ค์ด๊ฐ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์๋ค.
(2) Dictionary ๋ก ๋ณํ (์ง๋ฅํ Dictionary ์ฌ์ฉ)
n_code1 = {country: code for country, code in NA_CODES}
n_code2 = {country.upper() : code for country, code in NA_CODES}
n_code2๋ key๊ฐ์ ๋๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ๊น์ง ํ ๊ฒ.
- Dict Setdefault
source = (('k1', 'val1'),
('k1', 'val2'),
('k2', 'val3'),
('k2', 'val4'),
('k2', 'val5'))
new_dict1 = {}
new_dict2 = {}
(1) No use setdefault
for k, v in source:
if k in new_dict1:
new_dict1[k].append(v)
else:
new_dict1[k] = [v]
print('EX3-1 - ', new_dict1)
์ค๋ณต๋๋ key๊ฐ ์์ผ๋ฉด, ๋ฆฌ์คํธ์ appendํด์ dict๋ฅผ ๋ง๋๋ ์ฝ๋์ด๋ค.
(2) Use setdefault ( ์ด๊ฒ์ ์ฑ๋ฅ์ด ๋ ์ข๋ค๊ณ ํ๋ค. )
for k, v in source:
new_dict2.setdefault(k, []).append(v)
print('EX3-2 - ', new_dict2)
setdefault๋ฅผ ์ฌ์ฉํ๋ฉด if๋ฌธ์ ์ฌ์ฉํ์ง ์๊ณ ๋ ์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ํ์ค๋ก ๊ตฌํํ ์๊ฐ ์๋ค.
- ์ฌ์ฉ์ ์ ์ dict ์์
class UserDict(dict):
def __missing__(self, key):
print('Called : __missing__')
if isinstance(key, str):
raise KeyError(key)
return self[str(key)]
def get(self, key, default=None):
print("Called : __getitem__")
try:
return self[key]
except KeyError:
return default
def __contains__(self, key):
print("Called : __contains__")
return key in self.keys() or str(key) in self.keys()
dict๋ฅผ ์์๋ฐ์์ ํด๋์ค๋ฅผ ์์ฑํ ์ ์๋ค.
user_dict1 = UserDict(one=1, two=2)
user_dict2 = UserDict({"one" : 1, "two" : 2})
user_dict3 = UserDict([('one', 1), ('two', 2)])
์ด๋ ๊ฒ ๋์ ๋๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ค ์ ์๋ค.
print('EX4-1 - ', user_dict1, user_dict2, user_dict3)
print('EX4-2 - ', user_dict2.get("two"))
print('EX4-3 - ', 'one' in user_dict3)
# print('EX4-4 - ', user_dict3['three'])
print('EX4-5 - ', user_dict3.get('three'))
print('EX4-6 - ', 'three' in user_dict3)
์ฃผ์์ฒ๋ฆฌํ EX4-4 ๋ฅผ ์คํํ๋ฉด
__missing__ ์ด ํธ์ถ๋๋ฉด์ ์์์ ์ ์ํ๋ ํค์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค.
- Immutable Dict
from types import MappingProxyType
d = {'key1' : 'TEST1'}
# Read Only
d_frozen = MappingProxyType(d)
print('EX5-1 - ', d, id(d))
print('EX5-2 - ', d_frozen, id(d_frozen))
print('EX5-3 - ', d is d_frozen, d == d_frozen)
# d_frozen['key1'] = 'TEST2' # ===> ๋ฐ๋์ง ์์
# ์๋ณธ์ ์์ ๊ฐ๋ฅ
d['key2'] = 'TEST2'
print('EX5-4 - ', d)
d์ d_frozen ์ ๋ค๋ฅธ ๊ฐ์ฒด (๋ค๋ฅธ id) ์ด๊ณ , ๊ฐ(๋ด์ฉ)์ ๊ฐ๋ค๊ณ ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
d_frozen์ ์์ ํ๋ ค๊ณ ์๋ํ๋ฉด ์๋ฌ๊ฐ ๋๋ค. ์ฝ๊ธฐ๋ง ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
์๋ณธ์ธ d๋ ์์ (์ถ๊ฐ, ์ญ์ , ๋ณ๊ฒฝ)์ด ๊ฐ๋ฅํ๋ค.
- Set
s1 = {'Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'}
s2 = set(['Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'])
s3 = {3}
s4 = set() # Not {}
s5 = frozenset({'Apple', 'Orange', 'Apple', 'Orange', 'Kiwi'})
ํ๊ฐ์ง ์ฃผ์ํ ์ ์ ๋น์ด์๋ set๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ set() ์ ์ฌ์ฉํ์ฌ์ผ ํ๋ค. {} ์ผ๋ก ์์ฑํ๋ฉด dict ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค.
# ์ถ๊ฐ
s1.add('Melon')
# ์ถ๊ฐ ๋ถ๊ฐ
# s5.add('Melon')
print('EX6-1 - ', s1, type(s1))
print('EX6-2 - ', s2, type(s2))
print('EX6-3 - ', s3, type(s3))
print('EX6-4 - ', s4, type(s4))
print('EX6-5 - ', s5, type(s5))
Immutable Dict์ ๊ฐ์ด frozenset์ผ๋ก ์์ ์ด ๋ถ๊ฐํ๊ฒ ๋ง๋ค ์ ์๋ค.
- ์ ์ธ ์ต์ ํ
a = {5} # ---> ์ด๊ฒ ๋ ๋น ๋ฅด๋ค.
b = set([10])
*** ์ฆ๋ช
from dis import dis
print('EX6-5 - ')
print(dis('{10}'))
print('EX6-6 - ')
print(dis("set([10])"))
dis๋ฅผ ์ฌ์ฉํ๋ฉด ์คํ๋๋ ์์๋ฅผ ๋ณผ ์ ์๋ค.
{10} ์ ์ ์ฐจ๊ฐ ๋ ์ ๋ค!
- ์ง๋ฅํ ์งํฉ (Comprehending Set)
from unicodedata import name
print('EX7-1 - ')
print({name(chr(i), "") for i in range(0, 256)})
๋์ ๋๋ฆฌ์ ๋น์ทํ๊ฒ for๋ฌธ์ {}์์ ๋ฃ์ด์ฃผ์ด ์ง๋ฅํ ์งํฉ์ ์์ฑํ ์ ์๋ค.