ABOUT ME

먹는걸 좋아하고 다양한 일을 하며 살고 싶어하는 파워J 개발자 블로그

Today
Yesterday
Total
  • collections 모듈 - Counter
    Engineering WIKI/Python 2022. 5. 12. 15:23
    • collections.Counter()의 결과값(return)은 딕셔너리 형태로 출력
    # collections.Counter 예제 (1)
    # list를 입력값으로 함
    import collections
    lst = ['aa', 'cc', 'dd', 'aa', 'bb', 'ee']
    print(collections.Counter(lst))
    '''
    결과
    Counter({'aa': 2, 'cc': 1, 'dd': 1, 'bb': 1, 'ee': 1})
    '''
    
    
    # collections.Counter 예제 (2)
    # dictionary를 입력값으로 함
    import collections
    print(collections.Counter({'가': 3, '나': 2, '다': 4}))
    '''
    결과
    Counter({'다': 4, '가': 3, '나': 2})
    '''
    
    # collections.Counter 예제 (3)
    # '값=개수' 입력값으로 함
    import collections
    c = collections.Counter(a=2, b=3, c=2)
    print(collections.Counter(c))
    print(sorted(c.elements()))
    '''
    결과
    Counter({'b': 3, 'c': 2, 'a': 2})
    ['a', 'a', 'b', 'b', 'b', 'c', 'c']
    '''
    
    # collections.Counter 예제 (4)
    # '문자열'을 입력값으로 함
    import collections
    container = collections.Counter()
    container.update("aabcdeffgg")
    print(container)
    '''
    결과
    Counter({'f': 2, 'g': 2, 'a': 2, 'e': 1, 'b': 1, 'c': 1, 'd': 1})
    '''
    for k,v in container.items():
    print(k,':',v)
    '''
    결과
    f : 2
    e : 1
    b : 1
    g : 2
    c : 1
    a : 2
    d : 1
    '''
    
    

    update() 메소드 사용

    # collections.Counter 예제 (5)
    # update() 메소드 사용
    import collections
    # 문자열
    a = collections.Counter()
    print(a)
    a.update("abcdefg")
    print(a)
    '''
    결과
    Counter()
    Counter({'f': 1, 'e': 1, 'b': 1, 'g': 1, 'c': 1, 'a': 1, 'd': 1})
    '''
    # 딕셔너리
    a.update({'f':3, 'e':2})
    print(a)
    '''
    결과
    Counter({'f': 4, 'e': 3, 'b': 1, 'g': 1, 'c': 1, 'a': 1, 'd': 1})
    '''
    

    elements() 메소드 사용

    # collections.Counter 예제 (6)
    # elements() 메소드 사용
    import collections
    c = collections.Counter("Hello Python")
    print(list(c.elements()))
    print(sorted(c.elements()))
    '''
    결과
    ['n', 'h', 'l', 'l', 't', 'H', 'e', 'o', 'o', ' ', 'y', 'P']
    [' ', 'H', 'P', 'e', 'h', 'l', 'l', 'n', 'o', 'o', 't', 'y']
    '''
    c2 = collections.Counter(a=4, b=2, c=0, d=-2)
    print(sorted(c.elements()))
    '''
    결과
    ['a', 'a', 'a', 'a', 'b', 'b']
    '''
    

    most_common() 메소드 사용

    # collections.Counter 예제 (7)
    # most_common() 메소드 사용
    import collections
    c2 = collections.Counter('apple, orange, grape')
    print(c2.most_common())
    print(c2.most_common(3))
    '''
    결과
    [('a', 3), ('p', 3), ('e', 3), ('g', 2), (',', 2), ('r', 2), (' ', 2), ('n', 1), ('l', 1), ('o', 1)]
    [('a', 3), ('p', 3), ('e', 3)]
    '''
    

    subtract() 메소드 사용

    # collections.Counter 예제 (8)
    # subtract() 메소드 사용
    import collections
    c3 = collections.Counter('hello python')
    c4 = collections.Counter('i love python')
    c3.subtract(c4)
    print(c3)
    '''
    결과
    Counter({'l': 1, 'h': 1, 'n': 0, 't': 0, 'p': 0, 'e': 0, 'o': 0, 'y': 0, 'i': -1, 'v': -1, ' ': -1})
    '''
    c = Counter(a=4, b=2, c=0, d=-2)
    d = Counter(a=1, b=2, c=3, d=4)
    c.subtract(d)
    print(c)
    '''
    결과
    Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
    '''
    

    Counter의 덧셈

    # collections.Counter 예제 (9)
    # Counter의 덧셈
    import collections
    a = collections.Counter(['a', 'b', 'c', 'b', 'd', 'a'])
    b = collections.Counter('aaeroplane')
    print(a)
    print(b)
    print(a+b)
    '''
    결과
    Counter({'b': 2, 'a': 2, 'd': 1, 'c': 1})
    Counter({'a': 3, 'e': 2, 'n': 1, 'r': 1, 'o': 1, 'p': 1, 'l': 1})
    Counter({'a': 5, 'b': 2, 'e': 2, 'n': 1, 'l': 1, 'd': 1, 'r': 1, 'o': 1, 'p': 1, 'c': 1})
    '''
    

    Counter의 뺄셈

    # collections.Counter 예제 (10)
    # Counter의 뺄셈
    import collections
    a = collections.Counter('aabbccdd')
    b = collections.Counter('abbbce')
    print(a-b)
    '''
    결과
    Counter({'d': 2, 'c': 1, 'b': 1, 'a': 1})
    '''
    

    Counter의 교집합, 합집합

    # collections.Counter 예제 (11)
    # Counter의 교집합
    import collections
    a = collections.Counter('aabbccdd')
    b = collections.Counter('aabbbce')
    print(a & b)
    '''
    결과
    Counter({'b': 2, 'a': 2, 'c': 1})
    '''
    print(a | b)
    '''
    결과
    Counter({'b': 3, 'c': 2, 'd': 2, 'a': 2, 'e': 1})
    '''
    

    'Engineering WIKI > Python' 카테고리의 다른 글

    collections 모듈 - OrderedDict  (0) 2022.05.12
    collections 모듈 - namedtuple  (0) 2022.05.12
    collections 모듈 - deque  (0) 2022.05.12
    collections 모듈 - defaultdict  (0) 2022.05.12
    vars() 내장함수  (0) 2022.05.10
    파이썬 리스트 컴프리헨션  (0) 2022.05.04
    Python 데코레이터  (0) 2022.05.02
    파이썬 __ slots __  (0) 2022.04.28
Designed by Tistory.