Python/Fundamentals

python standard libraries

metamong 2022. 8. 22.

๐Ÿน 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}

 

๐Ÿน ์ถ”ํ›„ ๊ณ„์† ์—…๋Žƒ ์˜ˆ์ •!


* ์ธ๋„ค์ผ ์ถœ์ฒ˜) https://medium.com/javarevisited/5-reasons-to-learn-python-for-data-science-16a9d4c44d6d

* ๋‚ด์šฉ ์ถœ์ฒ˜) ์ด์ฝ”ํ…Œ 2021 https://youtube.com/playlist?list=PLRx0vPvlEmdAghTr5mXQxGpHjWqSz0dgC 

 

'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

๋Œ“๊ธ€