dictionary
리μ€νΈμ λμ
λ리 λΉκ΅
μΈλ±μ€λ₯Ό κΈ°λ°μΌλ‘ κ°μ μ μ₯νλ κ²
ν€λ₯Ό κΈ°λ°μΌλ‘ κ°μ μ μ₯νλ κ²
λ³μ = {
ν€ : κ°,
ν€ : κ°,
...
ν€ : κ°
}
dict_a = {
"name1" : "user1",
"name2" : "user2"
}
print(dict_a["name1"]) # user1
print(dict_a["name2"]) # user2
리μ€νΈμ λμ
λ리λ₯Ό κ°μΌλ‘ λ£κΈ°
dict_b = {
"dirctor" : ["μμλ","μ‘°"],
"cast" : ["μμ΄μΈλ§¨","νλ
Έμ€","νν¬","ν λ₯΄"]
}
print(dict_b) # {'dirctor': ['μμλ', 'μ‘°'], 'cast': ['μμ΄μΈλ§¨', 'νλ
Έμ€', 'νν¬', 'ν λ₯΄']}
print(dict_b["dirctor"]) # ['μμλ', 'μ‘°']
λμ
λ리μ μμμ μ κ·ΌνκΈ°
dictionary = {
"name": "7D 건쑰 λ§κ³ ",
"type": "λΉμ μ",
"ingredient": ["λ§κ³ ", "μ€ν", "λ©νμ€μν©μ°λνΈλ₯¨", "μΉμν©μμ"],
"origin": "ν리ν"
}
print("name:", dictionary["name"]) # name: 7D 건쑰 λ§κ³
print("type:", dictionary["type"]) # type: λΉμ μ
print("ingredient:", dictionary["ingredient"]) # ingredient: ['λ§κ³ ', 'μ€ν', 'λ©νμ€μν©μ°λνΈλ₯¨', 'μΉμν©μμ']
print("origin:", dictionary["origin"]) # origin: ν리ν
print("--------------------------------------------")
κ° λ³κ²½
dictionary["name"] = "8D 건쑰 λ§κ³ "
print("name:", dictionary["name"]) # name: 8D 건쑰 λ§κ³
λμ
λ리 μμμ μΆκ° : λμ
λ리[μλ‘μ΄ ν€] = μλ‘μ΄ κ°
dictionary = {}
print("μμ μΆκ° μ΄μ :", dictionary)
dictionary["name"] = "μλ‘μ΄ μ΄λ¦"
dictionary["head"] = "μλ‘μ΄ μ μ "
dictionary["body"] = "μλ‘μ΄ λͺΈ"
print("μμ μΆκ° μ΄ν:", dictionary)
λμ
λ리 μμμ μ κ±° : del
dictionary2 = {
"name": "7D 건쑰 λ§κ³ ",
"type": "λΉμ μ"
}
print("μμ μ κ±° μ΄μ :", dictionary2) # μμ μ κ±° μ΄μ : {'name': '7D 건쑰 λ§κ³ ', 'type': 'λΉμ μ'}
del dictionary2["name"]
del dictionary2["type"]
print("μμ μ κ±° μ΄ν:", dictionary2) # μμ μ κ±° μ΄ν: {}
λμ
λ리 λ΄λΆμ ν€κ° μλμ§ νμΈ
: in ν€μλλ₯Ό μ¬μ©νμ¬ λ΄λΆμ ν€κ° μλμ§ μλμ§ νμΈ
dictionary = {
"name": "7D 건쑰 λ§κ³ ",
"type": "λΉμ μ",
"ingredient": ["λ§κ³ ", "μ€ν", "λ©νμ€μν©μ°λνΈλ₯¨", "μΉμν©μμ"],
"origin": "ν리ν"
}
key = input("> μ κ·Όνκ³ μ νλ ν€: ")
if key in dictionary:
print(dictionary[key])
else:
print("μ‘΄μ¬νμ§ μλ ν€μ μ κ·Όνκ³ μμ΅λλ€.")
λμ
λ리 λ§λ€ λ μ£Όμν μ¬ν
Keyλ κ³ μ ν κ°μ΄λ―λ‘ μ€λ³΅λλ Key κ°μ μ€μ ν΄ λμΌλ©΄ νλλ₯Ό μ μΈν λλ¨Έμ§ κ²λ€μ΄ λͺ¨λ 무μ
a = {1:'a', 1:'b'}
print(a) # {1: 'b'}
Keyμ 리μ€νΈλ μΈ μ μλ€
a = {[1,2] : 'hi'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
λμ
λ리 κ΄λ ¨ ν¨μλ€
Key 리μ€νΈ λ§λ€κΈ° : keys
a = {'name': 'pey', 'phone': '0119993323', 'birth': '1118'}
print(a.keys()) # dict_keys(['name', 'phone', 'birth'])
# 리μ€νΈλ‘ λ°ννλ λ°©λ² : list
list(a.keys()) # ['name', 'phone', 'birth']
Value 리μ€νΈ λ§λ€κΈ° : values
a.values() # dict_values(['pey', '0119993323', '1118'])
Key, Value μ μ»κΈ° : items
a.items() # dict_items([('name', 'pey'), ('phone', '0119993323'), ('birth', '1118')])
Key: Value μ λͺ¨λ μ§μ°κΈ° : clear
a.clear()
print(a) # {}
Keyλ‘ Valueμ»κΈ° : get
a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
print(a.get('name')) # 'pey'
# μ‘΄μ¬νμ§ μλ ν€μ μ κ·Όνλ μν©μμ λλ²μ§Έ λμ² λ°©λ²μΌλ‘λ λμ
λ리μ get()
value = dictionary.get("none")
print("κ°:", value)
if value == None:
print("μ‘΄μ¬νμ§ μλ ν€μ μ κ·Όνμμ΅λλ€.")
ν΄λΉ Keyκ° λμ
λ리 μμ μλμ§ μ‘°μ¬νκΈ° : in
a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
'name' in a
# True