내장 함수
result = sum([1,2,3,4,5])
result = min(7,3,5,2)
result = max(7,3,5,2)
result = eval("(3+5) * 7")
result = sorted([9,1,8,5,4])
result = sorted([9,1,8,5,4], reverse=True)
result = sorted([('홍길동', 35), ('이순신', 75), ('아무개', 50)], key=lambda x: x[1], reverse=True)
data = [9,1,8,5,4]
data.sort()
from itertools import permutations
result = list(permutations(data, 3))
from itertools import combinations
result = list(combinations(data, 2))
- product - 중복 포함 permutations
from itertools import product
result = list(product(data, repeat=2))
- combinations_with_replacement - 중복 포함 combinations
from itertools import combinations_with_replacement
result = list(combinations_with_replacement(data, repeat=2))
heapq
- 우선순위 큐 기능을 구현할 때
- 시간 복잡도: O(NlogN)
import heapq
heap.heappush(리스트, value)
heap.heappop(리스트)
heap.heappush(리스트, -value)
heap.heappop(리스트)
bisect
- left_value ≤ x ≤ right_value 원소의 개수 구하기
- 시간 복잡도: O(logN)
from bisect import bisect_left, bisect_right
def count_by_range(a, left_value, right_value):
right_index = bisect_right(a, right_value)
left_index = bisect_left(a, left_value)
return right_index - left_index
collections
- deque
- 인덱싱과 슬라이싱 불가
- 데이터 삽입, 삭제: O(1)
from collections import deque
data.appendleft(x)
data.append(x)
data.popleft()
data.pop()
from collections import Counter
counter = Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
counter['red']
counter['blue']
counter['green']
dict(counter)
math
import math
math.factorial(5)
import math
math.sqrt(7)
import math
math.gcd(21, 14)
import math
math.pi
math.e