Search
Duplicate

Dictionaries

νƒœκ·Έ
Dictionary
Introduction to Python Dictionaries
dictionary λŠ” key:value μ„ΈνŠΈλ‘œ λ˜μ–΄μžˆλ‹€. dictionaryλŠ” μ„œλ‘œ μ—°κ²°λœ 값을 λΉ λ₯΄κ²Œ 찾을 수 μžˆλ„λ‘ 데이터 쑰각을 μ„œλ‘œ λ§€ν•‘ν•˜λŠ” 방법을 μ œκ³΅ν•œλ‹€.
Make a Dictionary
dictionary λŠ” 숫자:숫자, λ¬Έμžμ—΄:숫자, λ¬Έμžμ—΄:λ°°μ—΄, μ•„λ‹ˆλ©΄ 이 λͺ¨λ“ κ²ƒμ„ μ„žμ–΄μ„œ λ§€ν•‘ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
Invalid Keys
Python의 dictionary λŠ” ν‚€μ˜ νŠΉμ • μ‹λ³„μžμΈ ν•΄μ‹œ 값이 μžˆλŠ” 각 킀에 μ˜μ‘΄ν•œλ‹€. ν‚€κ°€ 변경될 수 μžˆλŠ” 경우 ν•΄λ‹Ή ν•΄μ‹œ 값은 μ‹ λ’°ν•  수 μ—†κΈ° λ•Œλ¬Έμ— ν‚€λŠ” μˆ«μžλ‚˜ λ¬Έμžμ—΄κ³Ό 같이 항상 λ³€κ²½ λΆˆκ°€λŠ₯ν•˜κ³  ν•΄μ‹œ κ°€λŠ₯ν•œ 데이터 μœ ν˜•μ΄μ–΄μ•Ό ν•œλ‹€. 즉 μ•„λž˜μ™€ 같은 κ²½μš°μ—λŠ” TypeError κ°€ λ‚˜νƒ€λ‚  것이닀.
powers = {[1, 2, 4, 8, 16]: 2, [1, 3, 9, 27, 81]: 3} TypeError: unhashable type: 'list’
Python
볡사
Empty Dictionary
빈 dictionary λ₯Ό λ§Œλ“€ λ•Œμ˜ ꡬ문은 μ•„λž˜μ™€ κ°™λ‹€.
[dictionary λͺ…] = {}
Add A Key
(1) ν•˜λ‚˜μ˜ ν‚€λ₯Ό 더할 λ•Œ : [dictionary λͺ…][key] = value
(2) μ—¬λŸ¬κ°œμ˜ ν‚€λ₯Ό 더할 λ•Œ : [dictionary λͺ…].update({key1:value1, key2:value2 . . .})
Overwrite Values
이미 값이 μ •ν•΄μ ΈμžˆλŠ” key 의 값을 λ°”κΎΈκ³  μ‹Άλ‹€λ©΄ λ‹€μŒκ³Ό 같이 μž‘μ„±ν•˜λ©΄ λœλ‹€.
[dictionary λͺ…][key]
Dict Comprehensions
λ‘κ°œμ˜ dictionary λ₯Ό ν•©ν•˜κ³  μ‹Άλ‹€λ©΄ zip() 을 μ΄μš©ν•˜λ©° ꡬ문은 μ•„λž˜μ™€ κ°™λ‹€.
[dictionary λͺ…] = {key:value for key, value in zip([dictionary1], [dictionary2])}
[μ˜ˆμ‹œ μ½”λ“œ]
names = ['Jenny', 'Alexus', 'Sam', 'Grace'] heights = [61, 70, 67, 64] students = {key:value for key, value in zip(names, heights)} #students is now {'Jenny': 61, 'Alexus': 70, 'Sam': 67, 'Grace': 64}
Python
볡사
[μ˜ˆμ‹œ μ½”λ“œ]
drinks = ["espresso", "chai", "decaf", "drip"] caffeine = [64, 40, 0, 120] zipped_drinks = zip(drinks, caffeine) drinks_to_caffeine = {key: value for key, value in zipped_drinks}
Python
볡사
[μ˜ˆμ‹œ μ½”λ“œ]
songs = ["Like a Rolling Stone", "Satisfaction", "Imagine", "What's Going On", "Respect", "Good Vibrations"] playcounts = [78, 29, 44, 21, 89, 5] plays = {song:playcount for [song, playcount] in zip(songs, playcounts)} plays["Respect"] = 94 plays["Purple Haze"] = 1 library = {"The Best Songs": plays, "Sunday Feelings": {}} print(library)
Python
볡사
Get A Key
key 에 λŒ€ν•œ 정보λ₯Ό 가지고 있으면 주어진 key 에 λŒ€ν•œ 값에 μ ‘κ·Όν•  수 μžˆλ‹€.
[μ˜ˆμ‹œ μ½”λ“œ]
zodiac_elements = {"water": ["Cancer", "Scorpio", "Pisces"], "fire": ["Aries", "Leo", "Sagittarius"], "earth": ["Taurus", "Virgo", "Capricorn"], "air":["Gemini", "Libra", "Aquarius"]} print(zodiac_elements["earth"]) print(zodiac_elements["fire"]) # Output: # ['Taurus', 'Virgo', 'Capricorn'] # ['Aries', 'Leo', 'Sagittarius']
Python
볡사
Try/Except to Get a Key
νŠΉμ • ν‚€κ°€ μžˆλŠ”μ§€ μ—†λŠ”μ§€ 확인할 λ•Œ keyError λ₯Ό ν”Όν•˜λŠ” 방법이 μžˆλ‹€. λ°”λ‘œ Try/Except 문을 μ‚¬μš©ν•˜λŠ” 것이닀. ꡬ문은 μ•„λž˜μ™€ κ°™λ‹€.
try: νŠΉμ • ν‚€κ°€ μžˆμ„ λ•Œ μˆ˜ν–‰ ν•  ꡬ문 except keyError: νŠΉμ • ν‚€κ°€ 없을 λ•Œ μˆ˜ν–‰ ν•  ꡬ문
Python
볡사
Safely Get a Key
μ΄μ „μ—λŠ” KeyError λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ 사전에 ν‚€:κ°’ μŒμ„ μΆ”κ°€ν•˜λŠ” 것을 λ°°μ› λ‹€. ν•˜μ§€λ§Œ μ‚¬μš©μžκ°€ ν˜ΈμΆœν•  수 μžˆλŠ” λͺ¨λ“  ν‚€κ³Ό 값을 μ•Œμˆ˜λŠ” μ—†λ‹€. λ”°λΌμ„œ .get() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ key 에 μ ‘κ·Όν•˜λŠ” 것이 μ’‹λ‹€. λ§Œμ•½ .get() ν•˜λ €λŠ” ν‚€κ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ 기본적으둜 None 을 λ°˜ν™˜ν•œλ‹€. ꡬ문은 μ•„λž˜μ™€ κ°™λ‹€.
νŠΉμ • key 에 μ ‘κ·Όν•˜κ³  μ‹Άλ‹€λ©΄ : [dictionary λͺ…].get(’key’)
νŠΉμ • key 와 value 에 μ ‘κ·Όν•˜κ³  μ‹Άλ‹€λ©΄ : [dictionary λͺ…].get(’key’, β€˜value’)
[예제 μ½”λ“œ]
user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384} tc_id = user_ids.get("teraCoder", 100000) print(tc_id) # output : 100019 stack_id = user_ids.get("superStackSmash", 100000) print(stack_id) # output : 100000
Python
볡사
첫번째 κ²°κ³Όλ₯Ό 보면 이미 β€œteraCoder” λΌλŠ” ν‚€κ°€ 이미 dictionary 에 μ‘΄μž¬ν•˜κΈ° λ•Œλ¬Έμ— 값을 100000둜 지정 ν›„ .get() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν–ˆμ–΄λ„ μ›λž˜μ˜ 값인 100019 κ°€ λ‚˜μ˜¨λ‹€.
λ‘λ²ˆμ§Έ κ²°κ³Όλ₯Ό 보면 β€œsupserStackSmash” λΌλŠ” ν‚€λŠ” μ—†μœΌλ―€λ‘œ 좜λ ₯μ‹œμ—λŠ” μ§€μ •ν•œ 값이 κ·ΈλŒ€λ‘œ λ‚˜μ˜€κ²Œ λœλ‹€.
Delete a Key
νŠΉμ • key κ³Ό value λ₯Ό μ§€μš°κ³  μ‹Άλ‹€λ©΄ .pop() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. .pop() λ©”μ„œλ“œλŠ” μ§€μš΄ νŠΉμ • key 와 value λ₯Ό λ°˜ν™˜ν•œλ‹€λŠ” 것을 λͺ…μ‹¬ν•˜μž.
[예제 μ½”λ“œ]
available_items = {"health potion": 10, "cake of the cure": 5, "green elixir": 20, "strength sandwich": 25, "stamina grains": 15, "power stew": 30} health_points = 20 # νŠΉμ • key κ°€ dictionary 에 μ‘΄μž¬ν•˜λ©΄ dictionary μƒμ˜ 값이 λ°˜ν™˜λ˜κ³  dictionary μ—μ„œ μ§€μ›Œμ§„λ‹€. # λ§Œμ•½ μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ 0 이 λ°˜ν™˜λ˜μ–΄ 더해진닀. health_points += available_items.pop("stamina grains", 0) health_points += available_items.pop("power stew", 0) health_points += available_items.pop("mystic bread", 0) print(available_items) print(health_points) # output # {'health potion': 10, 'cake of the cure': 5, 'green elixir': 20, 'strength sandwich': 25} # 65
Python
볡사
Get All
(1) Get All Keys
list()
.keys()
β€’
κ·Έλƒ₯ μ‚¬μš©ν•˜λ©΄ list ν˜•μ‹μœΌλ‘œ λ°˜ν™˜λ˜κ³ 
β€’
λ°˜λ³΅λ¬Έμ— μ‚¬μš©ν•˜λ©΄ ν•œκ°œμ”© ν•œμ€„λ‘œ λ°˜ν™˜λœλ‹€.
(2) Get All Values
.values()
β€’
κ·Έλƒ₯ μ‚¬μš©ν•˜λ©΄ list ν˜•μ‹μœΌλ‘œ λ°˜ν™˜λ˜κ³ 
β€’
λ°˜λ³΅λ¬Έμ— μ‚¬μš©ν•˜λ©΄ ν•œμ€„μ”© λ°˜ν™˜λœλ‹€.
(3) Get All Items
.items()
각각의 dictionary μš”μ†Œλ₯Ό λ°˜ν™˜ν•΄μ€€λ‹€.
biggest_brands = {"Apple": 184, "Google": 141.7, "Microsoft": 80, "Coca-Cola": 69.7, "Amazon": 64.8} for company, value in biggest_brands.items(): print(company + " has a value of " + str(value) + " billion dollars. ") # Output # Apple has a value of 184 billion dollars. # Google has a value of 141.7 billion dollars. # Microsoft has a value of 80 billion dollars. # Coca-Cola has a value of 69.7 billion dollars. # Amazon has a value of 64.8 billion dollars.
Python
볡사