๐ก ๋ณธ ํฌ์คํ ์ ์์ ๋ด์ฉ, ๊ต์ฌ, ChatGPT๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ ๋ฆฌํ ํ์ต ๊ธฐ๋ก์ ๋๋ค.
๊ธ๊ณผ ๊ทธ๋ฆผ ๋ฑ ๋ชจ๋ ์ฝํ ์ธ ์ ์ ๋ฆฌ ๋ฐ ์์ฑ์ ๋ณธ ๋ธ๋ก๊ทธ์์ ์ง์ ์ ์๋์์ต๋๋ค.
Python ๊ธฐ์ด๋ถํฐ Streamlit ๋ฐ์ดํฐ ์๊ฐํ๊น์ง ์ ๋ฆฌํ๋ ํ์ต ๊ธฐ๋ก์ ๋๋ค.
์ด ๊ธ์ '5. [ํ์ด์ฌ] ๋์ ๋๋ฆฌ' ์ ๋๋ค.
๋์ ๋๋ฆฌ (Dictionary)
๋์ ๋๋ฆฌ๋ key์ value ๊ตฌ์กฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ์๋ฃํ์ด๋ค.
key → value
์๋ฅผ ๋ค์ด ํ์ ์ ์๋ฅผ ์ ์ฅํ ๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
โ๏ธ ๋์ ๋๋ฆฌ ์์
scores = {
"๊ตญ์ด": 90,
"์ํ": 85,
"์์ด": 95
}
print(scores)
๋์ ๋๋ฆฌ ์์ฑ ๋ฐฉ๋ฒ
๋์ ๋๋ฆฌ๋ ์ฌ๋ฌ ๋ฐฉ์์ผ๋ก ์์ฑํ ์ ์๋ค.
โ๏ธ ๊ธฐ๋ณธ ์์ฑ
person = {
"name": "ํ๊ธธ๋",
"age": 20,
"city": "Seoul"
}
print(person)
โ๏ธ dict() ์์ฑ
person = dict(name="ํ๊ธธ๋", age=20, city="Seoul")
print(person)
๋์ ๋๋ฆฌ ๊ฐ ์ ๊ทผ
๋์ ๋๋ฆฌ ๊ฐ์ 'key๋ฅผ ํตํด ์ ๊ทผ'ํ๋ค.
โ๏ธ key ์ ๊ทผ
scores = {
"๊ตญ์ด": 90,
"์ํ": 85
}
print(scores["๊ตญ์ด"])
get() ๋ฉ์๋
์กด์ฌํ์ง ์๋ key๋ฅผ ์กฐํํ ๋๋ get()์ ์ฌ์ฉํ๋ ๊ฒ์ด ์์ ํ๋ค.
โ๏ธ get() ์ฌ์ฉ
scores = {
"๊ตญ์ด": 90,
"์ํ": 85
}
print(scores.get("ํ๊ตญ์ฌ"))
print(scores.get("ํ๊ตญ์ฌ", 0))
๋์ ๋๋ฆฌ ๊ฐ ์ถ๊ฐ
๋์ ๋๋ฆฌ๋ mutable ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
โ๏ธ ๊ฐ ์ถ๊ฐ
scores = {
"๊ตญ์ด": 90,
"์ํ": 85
}
scores["์์ด"] = 95
print(scores)
๋์ ๋๋ฆฌ ๊ฐ ์์
โ๏ธ ๊ฐ ์์
scores["์ํ"] = 100
print(scores)
๋์ ๋๋ฆฌ ์ญ์
โ๏ธ ์ญ์
del scores["๊ตญ์ด"]
print(scores)
๋์ ๋๋ฆฌ ์ฃผ์ ๋ฉ์๋
โ๏ธ keys() : key๋ง ๋ถ๋ฌ์จ๋ค
print(scores.keys())
โ๏ธ values() : ๊ฐ๋ง ๋ถ๋ฌ์จ๋ค
print(scores.values())
โ๏ธ items() : key์ value๋ฅผ ๋ชจ๋ ๋ถ๋ฌ์จ๋ค
print(scores.items())
๋์ ๋๋ฆฌ ๋ฐ๋ณต๋ฌธ
๋์ ๋๋ฆฌ๋ ๋ฐ๋ณต๋ฌธ๊ณผ ํจ๊ป ์์ฃผ ์ฌ์ฉ๋๋ค.
โ๏ธ key ๋ฐ๋ณต
scores = {
"๊ตญ์ด": 90,
"์ํ": 85,
"์์ด": 95
}
for subject in scores:
print(subject)
โ๏ธ key + value ๋ฐ๋ณต
for subject, score in scores.items():
print(subject, score)
zip์ ์ด์ฉํ ๋์ ๋๋ฆฌ ์์ฑ
๋ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ฌถ์ด ๋์ ๋๋ฆฌ๋ฅผ ๋ง๋ค ์๋ ์๋ค.
subjects๋ฅผ key, scores๋ฅผ value๋ก zip ํ๋ค.
โ๏ธ zip ํ์ฉ
subjects = ["๊ตญ์ด", "์ํ", "์์ด"]
scores = [90, 85, 95]
result = dict(zip(subjects, scores))
print(result)
์คํ ๊ฒฐ๊ณผ
{'๊ตญ์ด': 90, '์ํ': 85, '์์ด': 95}
๋์ ๋๋ฆฌ ์ปดํ๋ฆฌํจ์ (Dictionary Comprehension)
๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ ์ฒ๋ผ ๋์ ๋๋ฆฌ๋ ์์ฑํ ์ ์๋ค.
โ๏ธ dict comprehension
numbers = [1, 2, 3, 4, 5]
square_dict = {x: x*x for x in numbers}
print(square_dict)
์คํ ๊ฒฐ๊ณผ
{1:1, 2:4, 3:9, 4:16, 5:25}
defaultdict
๊ธฐ๋ณธ๊ฐ์ ์๋์ผ๋ก ์์ฑํ๋ ๋์ ๋๋ฆฌ์ด๋ค.
โ๏ธ defaultdict ์์
from collections import defaultdict
word_count = defaultdict(int)
words = ["python", "java", "python", "c++"]
for word in words:
word_count[word] += 1
print(word_count)
์คํ ๊ฒฐ๊ณผ
{'python': 2, 'java': 1, 'c++': 1}
OrderedDict
์ฝ์ ์์๋ฅผ ์ ์งํ๋ ๋์ ๋๋ฆฌ์ด๋ค.
โ๏ธ OrderedDict ์์
from collections import OrderedDict
od = OrderedDict()
od["a"] = 1
od["b"] = 2
od["c"] = 3
print(od)
์คํ ๊ฒฐ๊ณผ
OrderedDict({'a': 1, 'b': 2, 'c': 3})
โ๏ธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์์ : ํ์ ์ฑ์ ๋ฐ์ดํฐ ๋ถ์
scores = {
"์ฒ ์": 85,
"์ํฌ": 92,
"๋ฏผ์": 78,
"์ง์": 95
}
total = 0
for name, score in scores.items():
total += score
average = total / len(scores)
print("ํ๊ท ์ ์:", average)
top_student = max(scores, key=scores.get)
print("์ต๊ณ ์ ์ ํ์:", top_student)
์คํ ๊ฒฐ๊ณผ
ํ๊ท ์ ์: 87.5
์ต๊ณ ์ ์ ํ์: ์ง์
๋ง๋ฌด๋ฆฌ
์ด๋ฒ ๊ธ์์๋ Python์ ํต์ฌ ์๋ฃ๊ตฌ์กฐ์ธ ๋์ ๋๋ฆฌ๋ฅผ ์ ๋ฆฌํ๋ค.
- dict ์์ฑ
- key / value
- dict ์ ๊ทผ
- dict ์์
- dict ๋ฐ๋ณต๋ฌธ
- zip
- dict comprehension
- defaultdict
- OrderedDict
๋์ ๋๋ฆฌ๋ Python์์ ๋งค์ฐ ์์ฃผ ์ฌ์ฉ๋๋ ์๋ฃ๊ตฌ์กฐ์ด๋ฉฐ
๋ฐ์ดํฐ ์ฒ๋ฆฌ, API ์๋ต, JSON ๋ฐ์ดํฐ ๋ฑ ๋ค์ํ ์ํฉ์์ ํ์ฉ๋๋ค.
'Python & SQL > Python Basics' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| 7. [ํ์ด์ฌ] ํจ์ ๊ธฐ์ด์ ๋งค๊ฐ๋ณ์ (0) | 2026.03.07 |
|---|---|
| 6. [ํ์ด์ฌ] set๊ณผ frozenset (0) | 2026.03.07 |
| 4. [ํ์ด์ฌ] ํํ๊ณผ ํจํน/์ธํจํน (0) | 2026.03.06 |
| 3. [ํ์ด์ฌ] Python ์๋ฃํ (0) | 2026.03.06 |
| 2. [ํ์ด์ฌ] ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ (0) | 2026.03.06 |