728x90
๋ฐ์ํ
โป SK ์ด๋์ค ๋ฃจํค์ฆ 24๊ธฐ (ํด๋ผ์ฐ๋๊ธฐ๋ฐ ์ค๋งํธ ์ตํฉ๋ณด์ ๊ณผ์ ) ๊ต์ก์ ๋ค์ผ๋ฉด์ ์์ฑํ ๋ด์ฉ์ ๋๋ค.
1๏ธโฃ ๋ฆฌ์คํธ(List)
๐น1. ์ ์
- ๋ฆฌ์คํธ๋ ์์๊ฐ ์๋ ๋ฐ์ดํฐ์ ์งํฉ
- ๋๊ดํธ
[ ]
๋ฅผ ์ฌ์ฉํด ์ ์ํ๋ฉฐ, ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ์ ์ฅ ํ ์ ์์
๐น2. ํน์ง
- ์์ ์ ์ง: ๋ฐ์ดํฐ๊ฐ ์ฝ์ ๋ ์์๋ฅผ ๊ธฐ์ตํจ (์ธ๋ฑ์ค๋ฅผ ํตํด ์ ๊ทผ ๊ฐ๋ฅ)
- ๊ฐ๋ณ์ฑ: ๋ฆฌ์คํธ์ ํฌ๊ธฐ์ ๋ด์ฉ์ ์์ ๋กญ๊ฒ ๋ณ๊ฒฝ ๊ฐ๋ฅ
- ์ค๋ณต ํ์ฉ: ๋์ผํ ๊ฐ์ด ์ฌ๋ฌ ๋ฒ ์ ์ฅ๋ ์ ์์
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, [10, 20]]
๐น3. ๋ฆฌ์คํธ ์ฃผ์ ๋ฉ์๋
๋ฆฌ์คํธ ์์ ์์
- ๋ฆฌ์คํธ์ ํน์ ์์๋ฅผ ์์ ํ๋ ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋จ
- ๋ฆฌ์คํธ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์๋ฃํ์ด๊ธฐ ๋๋ฌธ์ ์ํ๋ ์์๋ฅผ ์ง์ ๋ฐ๊ฟ ์ ์์
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango" # ๋ ๋ฒ์งธ ์์ ๊ฐ์ mango๋ก ์์
print(fruits) # ๊ฒฐ๊ณผ: ["apple", "mango", "cherry"]
๋ฆฌ์คํธ ๊ฒฐํฉ
- ๋ ๊ฐ ์ด์์ ๋ฆฌ์คํธ๋ฅผ ๊ฒฐํฉํ๋ ค๋ฉด
+
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํจ - ์ด ์ฐ์ฐ์๋ ๊ธฐ์กด์ ๋ฆฌ์คํธ๋ค์ ๋ณ๊ฒฝํ์ง ์๊ณ , ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ๋ฐํ
fruits = ["apple", "banana", "cherry"]
desserts = ["chocolate", "candy", "cake"]
food = fruits + desserts
print(food) # ์ถ๋ ฅ: ["apple", "banana", "cherry", "chocolate", "candy", "cake"]
๋ฆฌ์คํธ ์์ ์ถ๊ฐ ๋ฐ ์ญ์
- ๋ฆฌ์คํธ์ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋๋ค
- ์ถ๊ฐ: append(), insert()
- ์ญ์ : remove(), del
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # ๋ฆฌ์คํธ ๋์ "orange" ์ถ๊ฐ
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
fruits.insert(1, "pear") # ์ธ๋ฑ์ค 1์ 9 ์ฝ์
print(fruits) # ['apple', 'pear', 'banana', 'cherry', 'orange']
fruits.remove('pear') # ๊ฐ 9 ์ญ์
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
del fruits[0] # ์ฒซ ๋ฒ์งธ ์์ ์ญ์
print(fruits) # ['banana', 'cherry', 'orange']
๋ฆฌ์คํธ์์ ํน์ ์์ ์์น ์ฐพ๊ธฐ
- ํน์ ์์์ ์์น(์ธ๋ฑ์ค)๋ฅผ ์ฐพ์ผ๋ ค๋ฉด
index()
๋ฉ์๋๋ฅผ ์ฌ์ฉํจ list.index(element)
ํํ๋ก ์ฌ์ฉ๋๋ฉฐ, ์ฌ๊ธฐ์ element๋ ๋ฆฌ์คํธ์์ ์ฐพ๊ณ ์ ํ๋ ์์
# ๋ฆฌ์คํธ ์์ฑ
fruits = ["apple", "banana", "cherry"]
# ํน์ ์์์ ์์น ์ฐพ๊ธฐ
element = "banana"
index = my_list.index(element)
print(f"{element}์ ์์น๋ {index}์
๋๋ค.") # banana์ ์์น๋ 1์
๋๋ค.
๐น4. ๋ฆฌ์คํธ ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ
- ๋ฆฌ์คํธ์ ํน์ ์์น์ ์ ๊ทผํ๋ ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉ
- ์ฌ๋ผ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ์์๋ฅผ ์ ํ
print(numbers[0]) # ์ฒซ ๋ฒ์งธ ์์
print(numbers[-1]) # ๋ง์ง๋ง ์์
print(numbers[2:4]) # ์ธ๋ฑ์ค 2๋ถํฐ 3๊น์ง ์์
2๏ธโฃ ๋์ ๋๋ฆฌ
๐น1. ์ ์
- ๋์
๋๋ฆฌ๋ ํค
key
์ ๊ฐvalue
์ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ - ์ค๊ดํธ
{ }
๋ฅผ ์ฌ์ฉํด ์ ์ํ๋ฉฐ, ํค๋ ๊ณ ์ ํด์ผ ํ๊ณ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ ๊ฐ์ด์ด์ผ ํจ
๐น2. ํน์ง
- ์์ ์ ์ง: Python 3.7๋ถํฐ ์ฝ์ ๋ ์์๋ฅผ ์ ์ง
- ๋น ๋ฅธ ์ ๊ทผ ์๋: ํค๋ฅผ ์ฌ์ฉํด ๊ฐ์ ๋น ๋ฅด๊ฒ ์ ๊ทผ ๊ฐ๋ฅ
- ์ค๋ณต ํค ์์: ๋์ผํ ํค๊ฐ ์ฌ๋ฌ ๋ฒ ์กด์ฌํ ์ ์์
# ๋น ๋์
๋๋ฆฌ ์์ฑ
empty_dict = {}
# ํค-๊ฐ ์ ์ ์ฅ
person = {"name": "Alice", "age": 25, "city": "Seoul"}
# ์ค์ฒฉ ๋์
๋๋ฆฌ
nested_dict = {"user1": {"name": "Alice"}, "user2": {"name": "Bob"}}
๐น 3. ๋์ ๋๋ฆฌ ์ฃผ์ ๋ฉ์๋
๋์ ๋๋ฆฌ value ์ป๊ธฐ
- ๋์ ๋๋ฆฌ์ ๊ฐ์ ์ ๊ทผํ๋ ๋ ๊ฐ์ง ์ฃผ์ ๋ฐฉ๋ฒ
- ์ง์ ์ธ๋ฑ์ฑ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ
get()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
person = {"name": "John", "age": 30, "city": "New York"}
print(person["name"])
person = {"name": "John", "age": 30, "city": "New York"}
print(person.get("name",0))
- ์ค๋ฅ ์ฒ๋ฆฌ: ์ง์ ์ธ๋ฑ์ฑ์ ํค๊ฐ ์์ ๋ **KeyError**๋ฅผ ๋ฐ์์ํค์ง๋ง,
get()
๋ฉ์๋๋ **None**์ ๋ฐํํ๊ฑฐ๋ ์ ํ์ ์ผ๋ก ๋ค๋ฅธ ๊ธฐ๋ณธ๊ฐ์ ๋ฐํ - ๊ธฐ๋ณธ๊ฐ ์ง์ :
get()
๋ฉ์๋๋ ํค๊ฐ ์์ ๊ฒฝ์ฐ ๋ฐํํ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ๊ฐ๋ฅ
person = {"name": "John", "age": 30, "city": "New York"}
# ์ง์ ์ธ๋ฑ์ฑ ์ฌ์ฉ
try:
print("Name:", person["name"]) # ์กด์ฌํ๋ ํค
print("Salary:", person["salary"]) # ์กด์ฌํ์ง ์๋ ํค
except KeyError:
print("KeyError: 'salary' key does not exist.")
# get() ๋ฉ์๋ ์ฌ์ฉ
print("\nUsing get() method:")
print("Name:", person.get("name")) # ์กด์ฌํ๋ ํค
print("Salary:", person.get("salary")) # ์กด์ฌํ์ง ์๋ ํค, None์ ๋ฐํ
print("Salary with default:", person.get("salary", "Not Available")) # ์กด์ฌํ์ง ์๋ ํค, ๊ธฐ๋ณธ๊ฐ "Not Available" ๋ฐํ
๋์ ๋๋ฆฌ ์์ ์ถ๊ฐ ๋ฐ ์์
- ํค๋ฅผ ์ด์ฉํด ์๋ก์ด ๊ฐ์ ์ถ๊ฐํ๊ฑฐ๋, ๊ธฐ์กด ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์
person["job"] = "Engineer" # ์ ์์ ์ถ๊ฐ
person["age"] = 26 # ๊ธฐ์กด ๊ฐ ์์
print(person) # {'name': 'Alice', 'age': 26, 'city': 'Seoul', 'job': 'Engineer'}
๋์ ๋๋ฆฌ ์์ ์ ๊ฑฐ
del
: ํน์ ํค-๊ฐ ์์ ์ญ์ pop()
๋ฉ์๋: ํค๋ฅผ ์ง์ ํด ์ญ์ ํ๊ณ ๊ฐ์ ๋ฐ
# del ์ฌ์ฉ
del person["city"]
print(person) # {'name': 'Alice', 'age': 26, 'job': 'Engineer'}
# pop ์ฌ์ฉ
job = person.pop("job")
print(job) # Engineer
print(person) # {'name': 'Alice', 'age': 26}
728x90
๋ฐ์ํ
'๐งฉ Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ๋ฐ์ดํฐ ํํ์ ์ํ ์ ๊ทํํ์ ์ ๋ฆฌ (0) | 2025.01.15 |
---|---|
[Python] ํ์ผ ๋ฐ ๋๋ ํฐ๋ฆฌ ์ ์ด (1) | 2025.01.14 |
[Python] ๊ธฐ๋ณธ ์ ์ถ๋ ฅ ๋ค๋ฃจ๊ธฐ (0) | 2025.01.13 |
[์ค๋ฅ ํด๊ฒฐ] googletrans ์ฌ์ฉ ์ค ์ค๋ฅ (0) | 2025.01.13 |
[Python] googletrans ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉํด์ ๊ฐ๋จํ ๋ฒ์ญ๊ธฐ ๋ง๋ค๊ธฐ (1) | 2025.01.13 |