μ½”λ“œ - 패캠 μˆ˜μ—… μ½”λ“œ μ°Έκ³  (패캠 μˆ˜μ—… 정리)

 

 

<이전 κΈ€>

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ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ κ°’ μ œκ±°κΉŒμ§€ κ°€λŠ₯ν•˜λ‹€. 

+ Recent posts