ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Library
    python/기본 2022. 5. 1. 17:22

     

    Standard Library

    https://docs.python.org/3/library/

    defaultdict 예제

    s = "abcdadbcaad"
    
    d = {}
    for c in s:
        if c not in d:
            d[c] = 0
        else:
            d[c] += 1
    print(d)  # {'a': 4, 'b': 2, 'c': 2, 'd': 3}
    
    d = {}
    for c in s:
        d.setdefault(c, 0)
        d[c] += 1
    print(d)  # {'a': 4, 'b': 2, 'c': 2, 'd': 3}
    
    ####################################################################
    from collections import defaultdict
    
    d = defaultdict(int)
    for c in s:
        d[c] += 1
    print(d)  # defaultdict(<class 'int'>, {'a': 4, 'b': 2, 'c': 2, 'd': 3})
    
    ####################################################################
    d = {}
    for c in s:
        d[c] = d.get(c, 0) + 1
    print(d)
    
    ####################################################################
    import collections
    import sys
    
    # 참조하는 라이브러리 path 확인
    print(sys.path)
    
    # 설치된 경로 확인
    print(collections.__file__)

     

    Third-party Library

    Intellij 의 경우 Project Structure 설정에서 추가할 수 있음

    'python > 기본' 카테고리의 다른 글

    Class  (0) 2022.05.01
    __name__ 과 __main__  (0) 2022.05.01
    Built-in Functions  (0) 2022.04.30
    setup.py 로 패키지 만들기  (0) 2022.04.30
    Module Import  (0) 2022.04.30

    댓글

Designed by Tistory.