Python/์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[python ์ž๋ฃŒ๊ตฌ์กฐ] ๋”•์…”๋„ˆ๋ฆฌ dictionary

์ฃผ์˜ ๐Ÿฑ 2022. 11. 7. 12:31
728x90
๋ฐ˜์‘ํ˜•

- ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•จ

- key์™€ value๋กœ ์ด๋ฃจ์–ด์ง

{key1: Value1,key2:Value2,,,}

 

key์—๋Š” hashableํ•œ ๊ฒƒ๋“ค์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ

value๋Š” ์ œํ•œ์ด ์—†์Œ

 

๋นˆ ๋”•์…”๋„ˆ๋ฆฌ ์„ ์–ธ

dict={}

 

์‹คํ–‰ ์˜ˆ์‹œ

dictionary={}
dictionary['first']=1
dictionary['tuple']=(1,2,3)
dictionary[(4,'tuple')]=1.5
print(dictionary)
###{'first': 1, 'tuple': (1, 2, 3), (4, 'tuple'): 1.5}

print(dictionary['tuple'])#(1, 2, 3)
print(dictionary[4,'tuple'])#1.5

dictionary['first']='one' #์ค‘๋ณต์€ ๋ฎ์–ด์”Œ์›Œ์ง
print(dictionary)
#{'first': 'one', 'tuple': (1, 2, 3), (4, 'tuple'): 1.5}


del dictionary['tuple']#์‚ญ์ œ
print(dictionary)
#{'first': 'one', (4, 'tuple'): 1.5}

 

 

d={'ํ•œ๊ตญ์ธ':2,'์˜๊ตญ์ธ':8,'ํ•€๋ž€๋“œ์ธ':5}
 
print(d.items()) #ํŠœํ”Œ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ๋ณ€ํ™˜
print(d.keys())
print(d.values())
print(2 in d.values())
 
result
dict_items([('ํ•œ๊ตญ์ธ', 2), ('์˜๊ตญ์ธ', 8), ('ํ•€๋ž€๋“œ์ธ', 5)])
dict_keys(['ํ•œ๊ตญ์ธ', '์˜๊ตญ์ธ', 'ํ•€๋ž€๋“œ์ธ'])
dict_values([2, 8, 5])
True

 

๋ฐ˜์‘ํ˜•