ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] collections + itertools: 코딩테스트·실무에 유용한 도구 모음
    업무 자동화/Python 2025. 12. 4. 16:02

    1. collections 모듈

    1-1. Counter – 항목 개수 세기

    from collections import Counter
    
    data = ["a", "b", "a", "c", "b", "a"]
    cnt = Counter(data)
    
    print(cnt)              # Counter({'a': 3, 'b': 2, 'c': 1})
    print(cnt["a"])         # 3
    print(cnt.most_common(1))  # [('a', 3)]

    활용:

    • 문자/단어 빈도 분석
    • 로그/이벤트 카운팅
    • 최빈값 찾기

    1-2. defaultdict – 기본값이 있는 dict

    from collections import defaultdict
    
    scores = [("kim", 10), ("lee", 20), ("kim", 30)]
    
    d = defaultdict(list)
    for name, score in scores:
        d[name].append(score)
    
    print(d)  # {'kim': [10, 30], 'lee': [20]}
    • 키가 없을 때 자동으로 초기값 생성 (여기서는 list())
    • 초기값 예시: list, int, set
    from collections import defaultdict
    
    count = defaultdict(int)
    for ch in "hello":
        count[ch] += 1
    print(count)  # {'h': 1, 'e': 1, 'l': 2, 'o': 1}

    1-3. deque – 양쪽에서 빠른 큐

    from collections import deque
    
    q = deque()
    
    q.append(1)
    q.append(2)
    q.appendleft(0)
    
    print(q)         # deque([0, 1, 2])
    
    print(q.popleft())  # 0
    print(q.pop())      # 2
    • 양쪽 끝 삽입/삭제가 O(1)에 가까움
    • 슬라이딩 윈도우, BFS, 로그 버퍼, 최근 N개 기록 등에 유용

    1-4. namedtuple – 간단한 레코드 타입

    from collections import namedtuple
    
    Point = namedtuple("Point", ["x", "y"])
    
    p = Point(10, 20)
    print(p.x, p.y)
    • 튜플처럼 가볍지만, 필드 이름으로 접근 가능
    • “클래스 만들기엔 오버킬, 튜플은 가독성이 떨어질 때” 사용

     

    2. itertools 모듈

    2-1. product – 데카르트 곱 (모든 조합)

    from itertools import product
    
    colors = ["red", "blue"]
    sizes = ["S", "M", "L"]
    
    for c, s in product(colors, sizes):
        print(c, s)
    • 복수 리스트의 모든 조합 생성
    • 옵션 조합, 좌표, 테스트 케이스 생성에 활용

    2-2. permutations – 순열

    from itertools import permutations
    
    items = [1, 2, 3]
    
    for p in permutations(items, 2):
        print(p)
    • 길이 r인 순열들을 생성 (r 생략 시 전체 길이)
    • 순서가 중요한 경우의 수 계산에 사용

    2-3. combinations – 조합

    from itertools import combinations
    
    items = [1, 2, 3]
    
    for c in combinations(items, 2):
        print(c)
    • 순서 상관 없이 선택할 경우
    • 부분집합, 후보 조합 탐색 등에 사용

    2-4. accumulate – 누적합 등

    from itertools import accumulate
    
    data = [1, 2, 3, 4]
    
    for s in accumulate(data):
        print(s)   # 1, 3, 6, 10
    • 기본은 누적 합계
    • accumulate(data, func)으로 누적 곱, 최대값, 최소값 등을 구할 수도 있음
    import operator
    from itertools import accumulate
    
    data = [1, 2, 3, 4]
    for p in accumulate(data, operator.mul):
        print(p)   # 1, 2, 6, 24

    2-5. groupby – 정렬된 데이터 그룹화

    from itertools import groupby
    
    data = [("A", 1), ("A", 2), ("B", 3), ("B", 4)]
    
    for key, group in groupby(data, key=lambda x: x[0]):
        values = [v for _, v in group]
        print(key, values)
    • 정렬된 상태에서 같은 키 연속 구간을 그룹화
    • 로그/레코드가 이미 키 기준으로 정렬되어 있다면 유용

     

    3. 함께 쓰는 실무 패턴

    3-1. 단어 빈도 상위 N개 출력

    from collections import Counter
    
    text = "this is a test this is only a test"
    words = text.split()
    
    counter = Counter(words)
    for word, cnt in counter.most_common(3):
        print(word, cnt)

    3-2. 조합과 카운터로 “합이 특정 값인 쌍” 찾기

    from itertools import combinations
    
    nums = [1, 3, 5, 7, 9]
    target = 10
    
    pairs = [
        (a, b)
        for a, b in combinations(nums, 2)
        if a + b == target
    ]
    
    print(pairs)

    3-3. deque로 최근 N개 로그 관리

    from collections import deque
    
    recent_logs = deque(maxlen=5)
    
    for i in range(10):
        recent_logs.append(f"log {i}")
    
    print(recent_logs)  # 마지막 5개만 유지
Designed by Tistory.