ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

[Python] ์ž๋ฃŒํ˜• - ๋”•์…”๋„ˆ๋ฆฌ Dictionary

NaNaRin๐Ÿ™ƒ 2021. 2. 4. 18:00

0. ๋”•์…”๋„ˆ๋ฆฌ Dictionary ๋ž€ ?

๋Œ€์‘ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•( ์—ฐ๊ด€ ๋ฐฐ์—ด Asssociative array ๋˜๋Š” ํ•ด์‹œ Hash ๋ผ๊ณ  ํ•จ )

Key์™€ Value๋ฅผ ํ•œ ์Œ์œผ๋กœ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•.

Value์—๋Š” ์•„๋ฌด ๊ฐ’์ด๋‚˜ ๋‹ค ๋„ฃ์„ ์ˆ˜ ์žˆ์ง€๋งŒ Key๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Key๋ฅผ ํ†ตํ•ด Value๋ฅผ ์–ป๋Š” ๋ฐฉ์‹

Key๋Š” ๊ณ ์œ ํ•œ ๊ฐ’์ด๋ฏ€๋กœ ์ค‘๋ณต๋˜๋Š” Key๊ฐ’์ด ์žˆ๋‹ค๋ฉด ํ•˜๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ ๋ฌด์‹œ๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค

 -> ๋™์ผํ•œ Key๊ฐ€ ์กด์žฌํ•˜๋ฉด Key๊ฐ’์„ ํ†ตํ•ด Value๋ฅผ ์–ป์„ ๋•Œ ์–ด๋–ค ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€์•ผ ํ• ์ง€ ๋ชจํ˜ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต์ด ๋ถˆ๊ฐ€๋Šฅ!

# ๋”•์…”๋„ˆ๋ฆฌ๋ช… = {ํ‚ค1:์š”์†Œ1, ํ‚ค2:์š”์†Œ2, ํ‚ค3:์š”์†Œ3, …}
# DictName = {'name':'Narin', 'age':'20', 'birth':'0101'}

a = {}
b = {1: 'hi'}
c = {'a': [1, 2, 3]}
d = {'abc': 2, 'def': 5, 'ghi': 1}

 

1. ๋”•์…”๋„ˆ๋ฆฌ ์ถ”๊ฐ€, ์‚ญ์ œ

๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ์ธ๋ฑ์Šค ๋Œ€์‹  key ๊ฐ’์„ ๋„ฃ์–ด ๋”•์…”๋„ˆ๋ฆฌ ์Œ์„ ์ถ”๊ฐ€, ์‚ญ์ œํ•œ๋‹ค

b[2] = 'my'
print(b)                            # {1: 'hi', 2: 'my'}
c['b'] = [4, 5, 6]
print(c)                            # {'a': [1, 2, 3], 'b': [4, 5, 6]}
d['jkl'] = 8
print(d)                            # {'abc': 2, 'def': 5, 'ghi': 1, 'jkl': 8}

del b[1]
print(b)                            # {2: 'my'}
del d['abc']
print(d)                            # {'def': 5, 'ghi': 1, 'jkl': 8}

 

2. ๋”•์…”๋„ˆ๋ฆฌ์—์„œ Key ๊ฐ’์„ ์ด์šฉํ•ด Value ์–ป๊ธฐ

๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์€ ์š”์†Œ๊ฐ’์„ ์–ป์„ ๋•Œ ์ธ๋ฑ์‹ฑ์ด๋‚˜ ์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ฟ์ด๋‹ค

print(b[2])                         # my
print(c['a'])                       # [1, 2, 3]
print(d['def'])                     # 5

 

3. ๋”•์…”๋„ˆ๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜

- keys() : key ๊ฐ’์„ ๋ชจ์•„ dict_keys ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค

- values() : value ๊ฐ’์„ ๋ชจ์•„ dict_values ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค

- items() : key, value ๊ฐ’ ์Œ์„ ํŠœํ”Œ๋กœ ๋ฌถ์€ dict_items ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค

dict_keys, dict_values, dict_items ๋ชจ๋‘ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์ด ๋ฐ˜๋ณต ๊ตฌ๋ฌธ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‚˜ ๋ฆฌ์ŠคํŠธ ๊ณ ์œ ์˜ ํ•จ์ˆ˜๋“ค์€ ์ˆ˜ํ–‰์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค

DictName = {'name': 'Narin', 'age': '20', 'birth': '0101'}

print(list(DictName.keys()))        # ['name', 'age', 'birth']
for i in DictName.keys():           # name
    print(i)                        # age
                                    # birth

print(list(DictName.values()))      # ['Narin', '20', '0101']
for i in DictName.values():         # Narin
    print(i)                        # 20
                                    # 0101

print((list(DictName.items())))     # [('name', 'Narin'), ('age', '20'), ('birth', '0101')]
for i in DictName.items():          # ('name', 'Narin')
    print(i)                        # ('age', '20')
                                    # ('birth', '0101')