주요 내용 요약
1. 내장함수(sum([]), min(,,,), max(,,,), eval(""), sorted())
2. itertools(permutations, combinations, product, combinations_with_replacement)
3. heapq(heapq.heappush(heap, value), heapq.heappop(heap)) : 오름차순
4. bisect(bisect_left(arr, 4), bisect_right(arr, 4)) : 정렬된 배열에서 특정 원소 찾기
5. collections(deque, Counter)
6. math(factorial, sqrt, gcd, pi, e)
: 'import math'만 하고 math.factorial(x) 이런 식으로 가져다 씀
from itertools import permutations
data = ['A', 'B', 'C']
result = list(permutations(data,3))
print(result)
=> [('A','B','C'),('A','C','B'), ('B','A','C'), ... ]
from itertools import combinations
data = ['A', 'B', 'C']
result = list(combinations(data,3))
print(result)
=> [('A','B','C')]
from itertools import product
data = ['A', 'B', 'C']
result = list(product(data,repeat=2))
print(result)
=> [('A','A'), ('A','B'), ('A','C'), ('B','A'),...]
from itertools import combinations_with_replacement
data = ['A', 'B', 'C']
result = list(combinations_with_replacement(data,2))
print(result)
=> [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C') , ('C', 'C')]
import heapq
arr = [2,5,1,7,3,9]
heap = []
result = []
for item in arr:
heapq.heappush(heap, item)
for i in range(len(heap)):
result.append(heapq.heappop(heap))
print(result)
from bisect import bisect_left, bisect_right
arr = [1,2,4,6,8]
print(bisect_left(arr,4))
print(bisect_right(arr,4))
=> 2, 3
등장횟수를 세는 기능 제공
from collections import Counter
counter = Counter(['red', 'red' , 'red', 'yellow', 'green', 'blue', 'blue'])
print(counter['red'])
print(dict(counter))
import math
print(math.factorial(5))
print(math.sqrt(36))
print(math.gcd(72,36))