ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Python

파이썬 λ”•μ…”λ„ˆλ¦¬(Dictionary)의 λͺ¨λ“  것

μ„œμ•„λž‘πŸ˜ 2023. 12. 4. 08:32

 

 

λ”•μ…”λ„ˆλ¦¬(Dictionary)

파이썬 λ”•μ…”λ„ˆλ¦¬λŠ” Key, Value ν•œμŒμœΌλ‘œ 이루어진 ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜ μ»¨ν…Œμ΄λ„ˆμž…λ‹ˆλ‹€. 리슀트, νŠœν”Œκ³ΌλŠ” λ‹€λ₯΄κ²Œ λ°μ΄ν„°μ˜ μˆœμ„œλŠ” μ€‘μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. νŠΉμ • Keyλ₯Ό 톡해 Valueλ₯Ό κ°€μ Έμ˜€λŠ” μ—°κ΄€ μ»¨ν…Œμ΄λ„ˆλΌλŠ” μ μž…λ‹ˆλ‹€. 리슀트, νŠœν”Œμ€ μ‹œν€€μŠ€ μ»¨ν…Œμ΄λ„ˆλΌκ³  ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

파이썬 λ”•μ…”λ„ˆλ¦¬λŠ” ν•΄μ‹œν…Œμ΄λΈ”(Hash Table)둜 κ΅¬μ„±λ˜μ–΄ 있기 λ•Œλ¬Έμ— λ°μ΄ν„°μ˜ 검색이 맀우 λΉ λ₯Έ νŽΈμž…λ‹ˆλ‹€. μ—¬κΈ°μ„œ μ€‘μš”ν•œ νŠΉμ§•μ€ Key의 κ³ μœ μ„±μž…λ‹ˆλ‹€. KeyλŠ” 쀑볡을 ν—ˆμš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ Key에 νŠœν”Œμ€ μ‚¬μš© κ°€λŠ₯ν•˜μ§€λ§Œ(immutable), λ¦¬μŠ€νŠΈλŠ” λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€(mutable).

 

κΈ°λ³Έ

dic = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
dic['name']    # 'seorang' -> value λ°˜ν™˜
dic['sex'] = 'male'    # μš”μ†Œ μΆ”κ°€
del dic['phone']    # μš”μ†Œ μ‚­μ œ

 

ν•¨μˆ˜

  1. Key만 뽑아내기(keys)
>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> a.keys()    
dict_keys(['name', 'phone', 'birth']   # dict_keys 객체 λ°˜ν™˜

>>>> list(a.keys())    
['name', 'phone', 'birth']    # 리슀트둜 λ³€ν™˜

>>>> for k in a.keys():
				print(k)

 

2. Value 리슀트 λ§Œλ“€κΈ°(values)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> a.values()
dict_values(['seoarang', '1313414', '0819']    # dict_values 객체 λ°˜ν™˜

 

3. Key, Value 쌍 μ–»κΈ°(items)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> a.items()
dict_items([('name', 'seoarang'), ('phone', '1313414'), ('birth', '0819')]) # dict_items 객체 λ°˜ν™˜

>>>> for key, val in a.items():
				print(f"key : {key}, value : {value}")

 

4. Key둜 Value μ–»κΈ°(get)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> a.get('name')
'seoarang'

>>>> a.get('age')
None    

>>>> a['age']
KeyError: 'nokey'   # μ—†λŠ” ν‚€λ₯Ό μ‘°νšŒν•  λ•Œ get방식은 None을 λ°˜ν™˜, a['sex']λŠ” Key 였λ₯˜ λ°œμƒ!

 

5. λ”•μ…”λ„ˆλ¦¬ λΉ„μš°κΈ°(clear)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> a.clear()
>>>> a
{}

 

6. Key 체크 ν•˜κΈ°(in)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> 'name' in a
True

>>>> 'age'
False

 

7. λ”•μ…”λ„ˆλ¦¬ 볡사(dict, copy)

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> b = a
>>>> a['name'] = 'david'
>>>> b
{'name' : 'david', 'phone':'1313414', 'birth': '0819'}   # dict1 = dict2의 경우 두 λ³€μˆ˜μ˜ λ©”λͺ¨λ¦¬ μ£Όμ†Œκ°€ 동일함

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> c = dict(a)
>>>> a['name'] = 'david'
{'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}   # dict(a)의 경우 aκ°€ λ³΅μ‚¬λ˜κΈ° λ•Œλ¬Έμ— μ„œλ‘œ λ‹€λ₯Έ λ©”λͺ¨λ¦¬ μ£Όμ†Œ

>>>> a = {'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}
>>>> d = a.copy()
>>>> a['name'] = 'david'
{'name' : 'seoarang', 'phone':'1313414', 'birth': '0819'}   # a.copy()의 경우 aκ°€ λ³΅μ‚¬λ˜κΈ° λ•Œλ¬Έμ— μ„œλ‘œ λ‹€λ₯Έ λ©”λͺ¨λ¦¬ μ£Όμ†Œ

 

8. μ΅œλŒ€κ°’, μ΅œμ†Œκ°’, ν•©(max, min, sum)

# Keyκ°’ κΈ°μ€€μœΌλ‘œ μ΅œλŒ€κ°’, μ΅œμ†Œκ°’, ν•© λ°˜ν™˜

>>>> a = {1 :'a', 2 : 'b', 3 : 'c'}
>>>> max(a)
3

>>> min(a)
1

>>>> sum(a)
6

>>>> a = {'a' : 1, 'b' : 2, 'c' : 3}
>>>> max(a)
'c'

>>> min(a)
'a'

>>>> sum(a)
TypeError : unsupported operand type(s) for +: 'int' and 'str'

 

 

μ°Έκ³ : 점프 투 파이썬