Dictionary

์‚ฌ๋žŒ์€ ๋ˆ„๊ตฌ๋“ ์ง€ โ€œ์ด๋ฆ„โ€ = โ€œํ™๊ธธ๋™โ€, โ€œ์ƒ์ผโ€ = โ€œ๋ช‡ ์›” ๋ช‡ ์ผโ€ ๋“ฑ์œผ๋กœ ๊ตฌ๋ถ„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์˜๋ฆฌํ•˜๊ฒŒ๋„ ์ด๋Ÿฌํ•œ ๋Œ€์‘ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์š”์ฆ˜ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด๋“ค๋„ ์ด๋Ÿฌํ•œ ๋Œ€์‘ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒํ˜•์„ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ์—ฐ๊ด€ ๋ฐฐ์—ด(Associative array) ๋˜๋Š” ํ•ด์‹œ(Hash)๋ผ๊ณ  ํ•œ๋‹ค.

ํŒŒ์ด์ฌ์—๋Š” ์ด ์ž๋ฃŒํ˜•์„ ๋”•์…”๋„ˆ๋ฆฌ๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ Key, Value ๋ผ๋Š” ๊ฒƒ์„ ํ•œ ์Œ์œผ๋กœ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•์ด๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ํŠœํ”Œ์ด๋‚˜ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ€์žฅ ๋‹ค๋ฅธ ์ ์ด๋ผ๋ฉด, ์ˆœ์ฐจ์ ์œผ๋กœ ํ•ด๋‹น ์š”์†Œ๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  Key ๋ฅผ ํ†ตํ•ด Value๋ฅผ ์–ป๋Š”๋‹ค๋Š” ์ ์ด๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค ๋’ค์ ธ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , Key๊ฐ€ ์žˆ๋Š” ๊ณณ๋งŒ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

Key list

>>> a = {โ€™nameโ€™: โ€™peyโ€™, โ€™phoneโ€™: โ€™0119993323โ€™, โ€™birthโ€™: โ€™1118โ€™}
>>> a.keys()
dict_keys([โ€™nameโ€™, โ€™phoneโ€™, โ€™birthโ€™])

Items

>>> a.items()
dict_items([(โ€™nameโ€™, โ€™peyโ€™), (โ€™phoneโ€™, โ€™0119993323โ€™), (โ€™birthโ€™, โ€™1118โ€™)])

items ํ•จ์ˆ˜๋Š” key์™€ value์˜ ์Œ์„ ํŠœํ”Œ๋กœ ๋ฌถ์€ ๊ฐ’์„ dict items ๊ฐ์ฒด๋กœ ๋Œ๋ ค์ค€๋‹ค.

Get

Key ๋กœ Value ๋ฅผ ์–ป์–ด๋ณด์ž.

>>> a = {โ€™nameโ€™:โ€™peyโ€™, โ€™phoneโ€™:โ€™0119993323โ€™, โ€™birthโ€™: โ€™1118โ€™}
>>> a.get(โ€™nameโ€™)
โ€™peyโ€™
>>> a.get(โ€™phoneโ€™)
โ€™0119993323โ€™

์„ ์–ธ ๋ฐฉ๋ฒ• / Value ์ ‘๊ทผ

# ์„ ์–ธ
author = {"python" : "person1", "c++" : "person2"}
 
# ์ ‘๊ทผ
author["python"]
 
# ์‚ญ์ œ
del author["python"]
 
# ์ถ”๊ฐ€
author["python"] = "person1"
 
for item in author:
    print(item, "is designed by ", author[item])
c++ is designed by  person2
python is designed by  person1

์‹ค์Šต

Example 1

(a) ๋‹ค์Œ๊ณผ ๊ฐ™์ด list ํ•˜๋‚˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ, ๋‹ค๋ฅธ list๋Š” ์–ธ์–ด์˜ ๊ฐœ๋ฐœ์ž ์ด๋ฆ„์„ ๊ฐ–๋„๋ก ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

language = ["python", "c++", "javascript", "go"]
author = ["Guido van Rossum", "Bjarne Stroustrup", "Brendan Eich", "Robert Griesemer"]

(b) ํ•จ์ˆ˜ matingPairs()๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ, ์ž…๋ ฅ ํŒŒ๋ผ๋ฉ”ํƒ€๋กœ ์œ„์˜ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์„œ, ๊ฒฐ๊ณผ๋กค set ํƒ€์ž…์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.

(c) ํ•จ์ˆ˜ matingPairs()๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ๊ฐ’์„ ๊บผ๋‚ด์„œ ์–ธ์–ด ์ด๋ฆ„๋ณ„ ์ €์ž์˜ tuple์„ ๋งŒ๋“  ํ›„,

(d) ํ•จ์ˆ˜ matingPairs() ์•ˆ์˜ ๋‚ด๋ถ€ ๋ณ€์ˆ˜์ธ set ํƒ€์ž… ๋ฐ์ดํ„ฐ ํƒ€์ž…์— (c)์—์„œ ๋งŒ๋“  tuple์„ ์•„์ดํ…œ์œผ๋กœ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

(e) ๋ชจ๋“  ์–ธ์–ด์— ๋Œ€ํ•œ ์ €์ž ๋งคํ•‘๊ณผ, ์ด๋ฅผ set์— ๋„ฃ๋Š” ๊ณผ์ •์„ ๋งˆ์น˜๋ฉด, ํ•จ์ˆ˜ matingPairs()์€ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ set๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.

(f) ํ•จ์ˆ˜ matingPairs()์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

language = ["python", "c++", "javascript", "go"]
author = ["Guido van Rossum", "Bjarne Stroustrup", "Brendan Eich", "Robert Griesemer"]
 
def matingPairs(array1, array2):
    result_set = set()
    for i in range(len(array1)):
        temp_tuple = (array1[i], array2[i])
        result_set.add(temp_tuple)
    return result_set
 
 
matingPairs(language, author)
{('c++', 'Bjarne Stroustrup'),
 ('go', 'Robert Griesemer'),
 ('javascript', 'Brendan Eich'),
 ('python', 'Guido van Rossum')}

Example 2

๋‹ค์Œ์˜ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋งž๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜์—ฌ ์•„๋ž˜์˜ ์ž…๋ ฅ์ฐฝ์„ ํ†ตํ•ด์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

(a) dictionary์˜ key๋Š” ์œ ์ผํ•ด์•ผ ํ•˜์ง€๋งŒ value๋Š” ์œ ์ผํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

(b) count_values() ๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

(c) count_values() ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜์˜ dictionary๋ฅผ ์ž…๋ ฅ ํŒŒ๋ผ๋ฉ”ํƒ€๋กœ ๋ฐ›์•„์„œ, ์ด dictionary๊ฐ€ ํฌํ•จํ•œ ์„œ๋กœ ๋‹ค๋ฅธ value์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

(d) ์˜ˆ๋ฅผ ๋“ค์–ด, {'red': 1, 'green': 1, 'blue': 2}๊ฐ€ ์ž…๋ ฅ ํŒŒ๋ผ๋ฉ”ํƒ€๋กœ ์ „๋‹ฌ๋˜๋ฉด, 2๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

def count_values(dic):
    set_dic = set()
    for item in dic:
        set_dic.add(dic[item])
    return len(set_dic)
 
dic = {'red' : 1, 'green' : 1, 'blue' : 2}
 
count_values(dic)
2