๐Ÿ“‚์›น ๊ฐœ๋ฐœ(Web)/๐ŸํŒŒ์ด์ฌ(Python)

Python dict ์ด์ •๋ฆฌ(dictionary)

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 1. 25. 16:19

Dictionary

    Key : value๋กœ ์ด๋ฃจ์–ด์ ธ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ

 

1.  dictionary ์„ ์–ธ

thisdict = {

    "brand":"๋‚˜์ดํ‚ค",

    "model":"max",

    "year":1990

}

print(thisdict)

'''

ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

'''

 

2. ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

thisdict = {

    "brand":"๋‚˜์ดํ‚ค",

    "model":"max",

    "year":1990

}

print(thisdict["brand"]) #ํ‚ค ๊ฐ’ ์‚ฌ์šฉํ•ด์„œ value ๊ฐ€์ ธ์˜ค๊ธฐ

print(thisdict.get("model")) #get๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด์„œ value ๊ฐ€์ ธ์˜ค๊ธฐ

 

3. ํ‚ค ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ

print(thisdict.keys())

 

4.  ์ถ”๊ฐ€ํ•˜๊ธฐ1

thisdict = {

    "brand":"Ford",

    "model":"Mustang",

    "year":1964

}

thisdict["color"] = "red"

print(thisdict)

 

์ถ”๊ฐ€ํ•˜๊ธฐ2

thisdict.update({"bgColor":"black"})

print(thisdict)

 

5. ๋ณ€๊ฒฝํ•˜๊ธฐ

thisdict["color"] = "yellow"

thisdict.update({"bgColor":"blue"})

print(thisdict)

 

6. ์ œ๊ฑฐํ•˜๊ธฐ

thisdict.pop("model")

print(thisdict)

 

7. ๋งˆ์ง€๋ง‰ ์‚ฝ์ž…๋œ ํ•ญ๋ชฉ ์ œ๊ฑฐํ•˜๊ธฐ

thisdict.popitem()

print(thisdict)