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
볡μ¬