🌼 실전에서 자주 사용되는 표준 라이브러리

1. itertools: 반복되는 형태의 데이터 처리를 위한 함수 제공

  • ex) 순열 조합

2. heapq: 힙(Heap) 자료구조 제공

  • 일반적으로 우선순위큐 구현시 사용

3. bisect: 이진 탐색(Binary Search) 기능을 제공

4. collections: 데크(dequeue), 카운터(Counter) 등의 자료구조 제공

5. amth: 필수적인 수학적 기능 제공

  • ex) 팩토리얼, 최대 공약수(GDC), 최소 공배수(LCM), 파이(Pi) 등 제공

 

🌼 순열과 조합 - itertools 라이브러리

  1. 순열 (permutations)
    • 서로 다른 n개에서 서로 다른 r개를 선택하여 일렬로 정렬(순서 고려)
    • 중복고려 버전 > product()
  2. 조합 (combinations)
    • 서로 다른 n개에서 서로 다른 r개를 선택 (순서 고려 X)
    • 중복고려 버전 > combinations_with_replacement()

 

🌼 Counter - collections 라이브러리

  • 등장 횟수를 세는 라이브러리
from collections import Counter

arr = ['blue', 'blue', 'red', 'blue', 'red', 'yellow', 'blue']
counter = Counter(arr)

print(counter['red'])
print(counter['blue'])
print(counter['yellow'])
print(dict(counter))

# 출력 결과
# 2
# 4
# 1
# {'blue': 4, 'red': 2, 'yellow': 1}

 

🌼  최대공약수와 최소공배수 - math 라이브러리

  • 최대공약수 - GCD
  • 최소공배수 - LCM
728x90

+ Recent posts