μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리)
<μ΄μ κΈ>
https://silvercoding.tistory.com/35
[python κΈ°μ΄] 3. λ°μ΄ν° νμ (1) (μλ£ν) - list, tuple
μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리) <μ΄μ κΈ> https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutori..
silvercoding.tistory.com
1. λμ λ리 (Dictionary)
: μμ X, μ€λ³΅ X, μμ O, μμ O
: Key, Value νμμΌλ‘ λμ΄ μμ. (Json/ MongoDB)
- μ μΈ
a = {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101}
b = {0: 'Hello Python', 1: 'Hello Coding'}
c = {'arr': [1, 2, 3, 4, 5]}
print(type(a))
<class 'dict'>
λμ λ리 κ°μ²΄λ₯Ό μ μΈν΄ μ€λ€.
- μΆλ ₯
print(a['name'])
print(a.get('name'))
print(a.get('adress')) # μμ ν μ½λ© λ°©μ : μ΄κ±° μ°λ κ²μ κΆμ₯. μλ¬κ° μλκΈ° λλ¬Έ
print(c['arr'][1:3])
kIM
kIM
None
[2, 3]
λμ λ리μμ κ°μ κ°μ Έμ€κΈ° μν 2κ°μ§ λ°©λ²μ΄ μλ€. 1. ν€μ μ§μ μ κ·Ό , 2. get() μ¬μ©
1λ² λ°©λ²μ΄ νΈλ¦¬νμ§λ§, μλ ν€ κ°μ μ¬μ©νλ©΄ μ€λ₯κ° λκ² λλ€. 2λ² λ°©λ²μ μ¬μ©νλ©΄ μλ ν€ κ°μ λ£λλΌλ Noneμ λ°νν΄μ£ΌκΈ° λλ¬Έμ λ μμ ν μ½λ©μ΄ κ°λ₯νλ€.
- λμ λ리 μΆκ°
a['adress'] = 'Seoul'
print(a)
a['rank'] = [1, 3, 4]
a['rank2'] = (1, 2, 3)
print(a)
{'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul'}
{'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul', 'rank': [1, 3, 4], 'rank2': (1, 2, 3)}
keyμ valueλ₯Ό μ§μ μ μ΄μ£Όλ©΄ λλ€. 리μ€νΈμ νν λν μΆκ°κ° κ°λ₯νλ€.
- keys, values, items
# print(a.keys()[0]) # 리μ€νΈ μΈλ±μ€λ‘ μ κ·Ό λΆκ°
temp = list(a.keys()) # ---> νλ³ν ν΄μ£Όμ΄μΌ ν¨
print(temp)
print(temp[1:3])
['name', 'Phone', 'birth', 'adress', 'rank', 'rank2']
['Phone', 'birth']
a.keys() λ₯Ό μ¬μ©νλ©΄ λμ λ리 aμ keyκ°λ€λ§ λͺ¨μμ λμ€κ² λλ€. μΈλ» 보면 리μ€νΈμ²λΌ μκ²Όμ§λ§ 리μ€νΈλ₯Ό μ¬μ©νλ κ² μ²λΌ μΈλ±μ±μ΄λ μ¬λΌμ΄μ±μ ν μ μλ€. λ°λΌμ list()λ₯Ό μ΄μ©νμ¬ νλ³νμ ν΄μ£Όλ©΄ ν€ κ°λ€μ΄ λ΄κΈ΄ 리μ€νΈλ₯Ό μμ±ν μ μλ€.
print(a.values())
print(list(a.values()))
print(a.items())
print(list(a.items()))
dict_values(['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)])
['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)]
dict_items([('name', 'kIM'), ('Phone', '010-7777- 7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))])
[('name', 'kIM'), ('Phone', '010-7777-7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))]
values()μ items()λ λ§μ°¬κ°μ§λ€!
- ν€ μ‘΄μ¬ μ¬λΆ
print(1 in b)
print(2 in b)
print('name' in a)
True
False
True
μμ κ°μ΄ λμ λ리μμ inμ μ΄μ©νλ©΄ keyκ°μ΄ μ‘΄μ¬νλμ§μ λν μ¬λΆλ₯Ό boolean νμ μΌλ‘ λ°νν΄ μ€λ€.
2. μ§ν© (Set)
: μμ X, μ€λ³΅ X
- μ μΈ
a = set()
b = set([1, 2, 3, 4])
c = set([1, 4, 6, 6, 6])
* μ£Όμν μ μ a = { } μ κ°μ΄ μ μΈνλ©΄ λμ λ리 κ°μ²΄κ° λ§λ€μ΄ μ§λ―λ‘ λΉ μ§ν©μ λ§λ€ κ²½μ°μ a = set() μ ννλ‘ μ μΈν΄ μ£Όμ΄μΌ νλ€.
print(type(a))
print(c)
<class 'set'>
{1, 4, 6}
μμ κ°μ΄ μ€λ³΅μ νμ©νμ§ μλλ€!
- νλ³ν
: Set μλ£νμ μ£Όλ‘ λ³ννμ¬ μ¬μ©νλ€.
t = tuple(b)
print(t)
l = list(b)
print(l)
(1, 2, 3, 4)
[1, 2, 3, 4]
μ€λ³΅ κ°μ΄ λ€μ΄κ°μ§ μλλ‘ setμ μμ±ν ν ννμ΄λ 리μ€νΈ λ±μΌλ‘ λ³ννμ¬ μ¬μ©νλ μΌμ΄ λ§λ€κ³ νλ€.
* μ§ν© μ°μ° (κ΅μ§ν©, ν©μ§ν©, μ°¨μ§ν©)
s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])
μ°μ°μ μννκΈ° μν μ§ν©μ μ μΈνλ€.
- κ΅μ§ν© : intersection / &
print(s1.intersection(s2))
print(s1 & s2)
{4, 5, 6}
{4, 5, 6}
- ν©μ§ν© : union / |
print(s1.union(s2))
print(s1 | s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
- μ°¨μ§ν© : difference / -
print(s1.difference(s2))
print(s1 - s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3}
{1, 2, 3}
- μΆκ° & μ κ±°
s3 = set([7, 8, 10, 15])
s3.add(18)
s3.add(7)
print(s3)
{7, 8, 10, 15, 18}
addν¨μλ₯Ό μ΄μ©νμ¬ κ°μ μΆκ°ν μ μλ€.
s3.remove(15)
print(s3)
{7, 8, 10, 18}
removeν¨μλ₯Ό μ΄μ©νμ¬ κ° μ κ±°κΉμ§ κ°λ₯νλ€.
'νλ‘κ·Έλλ° μΈμ΄ > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python κΈ°μ΄] 6. νμ΄μ¬ νλ¦μ μ΄ (λ°λ³΅λ¬Έ) - for, while (0) | 2021.07.31 |
---|---|
[python κΈ°μ΄] 5. νμ΄μ¬ νλ¦μ μ΄ (μ μ΄λ¬Έ) - 쑰건문 if (0) | 2021.07.31 |
[python κΈ°μ΄] 3. λ°μ΄ν° νμ (1) (μλ£ν) - list, tuple (0) | 2021.07.30 |
[python κΈ°μ΄] 2. λ°μ΄ν° νμ Data Type - μ«μν, λ¬Έμμ΄ (0) | 2021.07.30 |
[python κΈ°μ΄] 1. print ν¨μ (0) | 2021.07.30 |