본문 바로가기

개발에 도움이 되는/Python

(3)
heapq - 요약 이진 트리 기반의 최소 힙 자료 구조 최댓값과 최솟값을 찾는 연산을 빠르게 함. 기본 = 최소 힙 # 모듈 선언 import heapq # heapq 선언 ex_heap = [] -> 보통 List 선언해준 뒤, heapq 내장함수를 이용하여 사용. # heap에 원소 push heapq.heappush(ex_heap , 추가할 원소) # heap에 원소 pop heapq.heappop(ex_heap) -> 최솟값이 pop 됨. # 기존 list를 heap으로 변환 heapq.heapify(ex_heap) ex) ex_heap = [3, 2, 1, 8, 5] heapq.heapify(ex_heap) print(ex_heap) -> [1, 2, 3, 5, 8] # 최대 힙 응용 heapq.heap..
itertools (순열, 조합, 중복 순열, 중복 조합) - 요약 순열 = permutations 조합 = combinations 중복 순열 = product 중복 조합 = combinations_with_replacement # 모듈 선언 순열 : from itertools import permutations 조합 : from itertools import combinations 중복 순열 : from itertools import product 중복 조합 : from itertools import combinations_with_replacement # 순열(permutations) 구현 permutations(순열을 구할 목록, 기준 개수) ex) ex_list = ['a', 'b', 'c'] ex_str = "abc" per_list = list(per..
defaultdict - 요약 dictionary key 생성 시 default로 설정한 값으로 초기화 해줌. # 모듈 선언 from collections import defaultdict # defaultdict 선언 dic_type = defaultdict(type) ex) dic_int = defaultdict(int) -> 새로 key를 만들면 default 값으로 value에 0이 들어감. dic_str = defaultdict(str) -> 새로 key를 만들면 default 값으로 value에 빈 값()이 들어감. dic_list = defaultdict(list) -> 새로 key를 만들면 default 값으로 value에 빈 list([])가 들어감. # default 값 변경 (lambda 이용) # dic..

반응형