๐น python์์ ์ค์ ์ผ๋ก ์ ์ฉํ๊ฒ ์ฌ์ฉํ๋ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ด๋ฒ ํฌ์คํ ์ ๊ธฐํ์ผ์ ์ ๋ฆฌํด๋ณด๋ ค ํ๋ค
Module & Package
1. Module [1] ํ์์ฑ ๋ฐ ์ ์ * ์ฝ๋๊ฐ ๋น์ฐํ ๊ธธ์ด์ง๋ ์ํฉ์์ ๋ชจ๋ ํจ์, ๋ณ์๋ฅผ ๊ตฌํํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ ๋๊ตฐ๊ฐ ๋ง๋ค์ด๋์ ํจ์, ๋ณ์ ๋ฑ์ ํ์ฉํด์ผ ํ๋ค * ๋ชจ๋ = ํน์ ๋ชฉ์ ์
sh-avid-learner.tistory.com
๐น ๋ชจ๋, ํจํค์ง๊ฐ ๋ฌด์์ธ์ง๋ ์๋จ ํฌ์คํ ์์ ๊ฐ๋ณ๊ฒ ๋ค๋ฃฌ ์ ์ด ์๋ค.
๐น libraries
์ข ๋ฅ | ์ค๋ช |
๋ด์ฅํจ์ | ๊ธฐ๋ณธ์ ์ธ ํจ์๋ค ์ ๊ณต (ํ์์ ์ธ ๊ธฐ๋ฅ ํฌํจ) (print, input() ๋ฑ๋ฑ) |
itertools | ๋ฐ๋ณต๋๋ ํํ์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ (ํนํ ์์ด, ์กฐํฉ) |
heapq | heap ์๋ฃ๊ตฌ์กฐ ์ ๊ณต (์ฃผ๋ก ์ฐ์ ์์ queue ๊ธฐ๋ฅ ๊ตฌํ) |
bisect | ์ด์ง ํ์ binary search ๊ธฐ๋ฅ ์ ๊ณต |
collections | deque, counter ๋ฑ์ ์ ์ฉํ ์๋ฃ๊ตฌ์กฐ ํฌํจ |
math | ํ์์ ์ธ ์ํ์ ๊ธฐ๋ฅ ์ ๊ณต (factorial, sqrt, GCD, ์ผ๊ฐํจ์ ๊ด๋ จ, pi constant ๋ฑ๋ฑ) |
* ๋ด์ฅํจ์>
→ ๋ํ์ ์ธ sum(), min(), max(), eval(), sorted()
(eval()์ ์์์ผ๋ก ํํ๋ ํ๋์ ์์ ์ค์ ์ ํํ๋ก ๊ณ์ฐํด ๋ฐํํด์ฃผ๋ ํจ์)
res = sum([1,2,3])
print(res) #6
min_res = min(1,2,3)
max_res = max(1,2,3)
print(min_res, max_res) #1, 3
res = eval("(3+4*2)*8")
print(res) #88
→ sorted()๋ default๊ฐ์ด ascending order์ด๋ฏ๋ก, reverse=True๋ฅผ ํด์ฃผ๋ฉด descending order๋ก ์ ๋ ฌ๋๋ค.
res = sorted([3,6,2,4,1,2,9])
reverse_res = sorted([3,6,2,4,1,2,9], reverse = True)
print(res, reverse_res) #[1, 2, 2, 3, 4, 6, 9] [9, 6, 4, 3, 2, 2, 1]
array = [('ryan', 95), ('charlie', 32), ('bethany', 38)]
res = sorted(array, key=lambda x :x[1], reverse=True) #descending order
print(res) #[('ryan', 95), ('bethany', 38), ('charlie', 32)]
* itertools>
๐น ์์ด๊ณผ ์กฐํฉ์ ์ํด ์ฃผ๋ก ์ฌ์ฉ๋จ
- ์์ด) ์๋ก ๋ค๋ฅธ n๊ฐ์์ ์๋ก ๋ค๋ฅธ r๊ฐ๋ฅผ ์ ํํด ์ผ๋ ฌ๋ก ๋์ดํ๋ ๊ฒ $_{n}P_{r}$
- ์กฐํฉ) ์๋ก ๋ค๋ฅธ n๊ฐ์์ ์์์ ์๊ด์์ด ์๋ก ๋ค๋ฅธ r๊ฐ๋ฅผ ์ ํํ๋ ๊ฒ $_{n}C_{r}$
โ ์์ด
from itertools import permutations
data = ['A', 'B', 'C']
res = list(permutations(data,3))
print(res)
#[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]
โก ์กฐํฉ
from itertools import combinations
data = ['A', 'B', 'C']
res = list(combinations(data,2))
print(res)
[('A', 'B'), ('A', 'C'), ('B', 'C')]
โข ์ค๋ณต์์ด - ์ค๋ณต์ผ๋ก repeat์ธ์๊ฐ์๋งํผ ๋ฝ์ ์ผ๋ ฌ๋ก ๋์ดํ๋ ๊ฐ์ง์ ๊ตฌํ๊ธฐ
from itertools import product
data = ['A', 'B', 'C']
res = list(product(data,repeat=2))
print(res)
#[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
โฃ ์ค๋ณต์กฐํฉ - ์ค๋ณต์ผ๋ก 2๋ฒ์งธ ์ธ์ ๊ฐ์๋งํผ ๋ฝ์ ์ ํํ๋ ๊ฒฝ์ฐ์ ์ ๊ตฌํ๊ธฐ
from itertools import combinations_with_replacement
data = ['A', 'B', 'C']
res = list(combinations_with_replacement(data,2))
print(res)
#[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
โค Counter
→ ํ์ด์ฌ Collections ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ Counter๋ ๋ฑ์ฅ ํ์๋ฅผ ์ธ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ์ฆ, ๋ฐ๋ณต ๊ฐ๋ฅํ(iterable) ๊ฐ์ฒด๊ฐ ์ฃผ์ด์ก์ ๋, ๋ด๋ถ์ ์์๊ฐ ๋ช ๋ฒ ๋ฑ์ฅํ๋ ์ง ์๋ ค์ค!
→ ๋ํ Counter์ ๋ฐํํ์ dictionary๋ก ๊ฐ ์์๋ฅผ key๋ก, value๊ฐ ๊ฐ ์์์ ๋ฑ์ฅ ํ์๊ฐ ๋ค์ด๊ฐ dictionary๊ฐ ๋ฐํ๋๋ค.
from collections import Counter
counter = Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
print(counter['blue'], counter['green']) #3 1
print(dict(counter)) #{'red': 2, 'blue': 3, 'green': 1}
'Python > Fundamentals' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(useful) Methods (0) | 2022.11.13 |
---|---|
ฮป ํํ์ (0) | 2022.08.21 |
list, string, tuple, dictionary, set (iterables) (0) | 2022.08.19 |
File/Exception/Log Handling (0) | 2022.07.14 |
python OOP (0) | 2022.07.07 |
๋๊ธ