๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ | Python

[Python] collections ๋ชจ๋“ˆ

by KASSID 2023. 1. 5.

๋ชฉ์ฐจ

    728x90

    Python์˜ ๋ชจ๋“ˆ ์ค‘ collections์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

    ์ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ List, Tuple, Dict๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

     

     ๊ฐ์ฒด ์ข…๋ฅ˜ 

    deque, Counter, OrderedDcit, defaultdict, namedtuple

     


     deque 

    - stack๊ณผ queue๋ฅผ ์ง€์›

    - List์— ๋น„ํ•ด์„œ ํšจ์œจ์ ์ธ(๋น ๋ฅธ) ์ž๋ฃŒ ์ €์žฅ ๋ฐฉ์‹ ์ง€์›!

     

    from collections import deque

    appendleft() : ๋ฆฌ์ŠคํŠธ์˜ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€

    deque_list = deque() # ๊ฐ์ฒด ์„ ์–ธ
    for i in range(5):
        deque_list.append(i)	# [0,1,2,3,4]
    deque_list.appendleft(10)	# ์•ž์— ์ถ”๊ฐ€
    
    deque_list
    >>> deque([10, 0, 1, 2, 3, 4])

     

    deque๋Š”

    rotate, reverse ๋“ฑ์˜ Linked List์˜ ํŠน์„ฑ์„ ์ง€์›ํ•œ๋‹ค.

    ๋˜ํ•œ, ๊ธฐ์กด list ํ˜•ํƒœ์˜ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค.

     

    rotate(n) : n๋งŒํผ rotate

    deque_list.rotate(1) # ํ•œ ์นธ์”ฉ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€๊ธฐ
    deque_list
    >>> deque([4, 10, 0, 1, 2, 3])

    extend() : ๋’ค์— ์ถ”๊ฐ€

    extendleft() : ์•ž์— ์ถ”๊ฐ€ but, ์ž…๋ ฅ ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ค€๋‹ค.

    deque_list.extend([5,6,7]) # ๋’ค์— ์ถ”๊ฐ€
    deque_list
    >>> deque([4, 10, 0, 1, 2, 3, 5, 6, 7])
    
    deque_list.extendleft([5,6,7]) # ์•ž์— ์ถ”๊ฐ€ but, ์ž…๋ ฅ ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ
    deque_list
    >>> deque([7, 6, 5, 4, 10, 0, 1, 2, 3])

     

     

    deque์˜ ์žฅ์ ์€

    ๊ธฐ์กด List๋ณด๋‹ค ํšจ์œจ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

    ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ๋กœ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

     

    ์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž!

    from collections import deque
    import time
    
    # 1. deque
    start_time = time.time()
    deque_list = deque()
    for i in range(1000):
        for i in range(1000):
            deque_list.append(i)
            deque_list.pop()
    print(time.time() - start_time, "seconds")
    
    # 2. general
    start_time = time.time()
    just_list = []
    for i in range(1000):
        for i in range(1000):
            just_list.append(i)
            just_list.pop()
    print(time.time() - start_time, "seconds")
    
    >>> 0.08 ... seconds
    >>> 0.26 ... seconds

    time ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฒฝ๊ณผ์‹œ๊ฐ„์„ ์ธก์ •ํ•ด๋ณธ ๊ฒƒ์ด๋‹ค.

     

    deque์™€ general์˜ ์‹œ๊ฐ„์ด

    ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ๋Š” ์•ฝ 3๋ฐฐ์ •๋„ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

     

    ์ฆ‰, deque๊ฐ€ ๋” ํšจ์œจ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ๊ฒƒ์ด๋‹ค!

     


     OrderedDict 

    - python 3.6 ๋ฏธ๋งŒ๊นŒ์ง€ dict๋Š” ์ž…๋ ฅํ•œ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์•˜๋‹ค.

      ๊ทธ์— ๋ฐ˜ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฐ์ฒด!

    (3.6๋ถ€ํ„ฐ๋Š” ์˜๋ฏธ๊ฐ€ ์—†์–ด์กŒ๋‹ค.)


     defualtdict 

    - Dict type์˜ ๊ฐ’์— ๊ธฐ๋ณธ ๊ฐ’์„ ์ง€์ •ํ•˜์—ฌ

    ์‹ ๊ทœ๊ฐ’ ์ƒ์„ฑ์‹œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

     

    from collections import defaultdict
    
    df = defaultdict(lambda : 0) # ํ•จ์ˆ˜ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•จ!
    df
    >>> defaultdict(<function __main__.default_value()>, {})

     

    df["first"]
    print(df)
    
    >>> defaultdict(<function <lambda> at 0x000001A50C58F160>, {'first': 0})

    ์œ„ ์ฒ˜๋Ÿผ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด 0์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ง€์ •์ด ๋œ๋‹ค.

     

    df["second"] = 9
    printdf)
    
    >>> defaultdict(<function __main__.default_value()>, {'first': 0,'second': 9})

    ์ง€์ •ํ•œ ๊ฒฝ์šฐ์—๋Š”

    ๊ทธ์— ๋”ฐ๋ฅธ ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค.

     

    ํ™œ์šฉ์˜ˆ์‹œ

    from collections import defaultdict
    
    df = defaultdict(lambda : 0)		# ๊ธฐ๋ณธ๊ฐ’ 0
    text = "aaabbcbbdbebabbc"			# ํ…์ŠคํŠธ
    text = ','.join(text).split(',')	# ๋ชจ๋“  ๊ธ€์ž ๋ถ„๋ฆฌ
    
    text
    >>> ['a', 'a', 'a', 'b', 'b', 'c', 'b', 'b', 'd', 'b', 'e', 'b', 'a', 'b', 'b', 'c']

     

    for i in text:	# text์˜ ๊ฐœ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ์„ธ์–ด์ค€๋‹ค.
        df[i] += 1
        
    df
    >>> {'a': 4, 'b': 8, 'c': 2, 'd': 1, 'e': 1})

    ์ด๋ ‡๊ฒŒ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ค„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    + OrderedDict์™€ sorted๋กœ ์ •๋ ฌํ•˜๊ธฐ

    for a,b in OrderedDict(sorted(df.items(), key=lambda t:t[1], reverse=True)).items():
        print(a,b)
    
    >>>
    b 8
    a 4
    c 2
    d 1
    e 1

     


     Counter 

    - Sequence type์˜ ๋ฐ์ดํ„ฐ ์š”์†Œ๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ dict ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜

    from collections import Counter

     

    c = Counter("abcde") # ๋ฌธ์ž์—ด
    c
    >>> Counter({'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}) # ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ ์„ธ์–ด์ค€๋‹ค.
    
    c = Counter(['a','b','c','d','e']) # ๋ฆฌ์ŠคํŠธ
    c
    >>> Counter({'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}) # ์š”์†Œ์˜ ์ˆ˜๋ฅผ ์„ธ์–ด์ค€๋‹ค.
    
    c = Counter({'a':1, 'b':2, 'c':2}) # dict
    c
    >>> Counter({'a': 1, 'b': 2, 'c': 2}) # ๊ทธ๋Œ€๋กœ ๋‚˜์˜จ๋‹ค.

    dict ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ จ ๋ฉ”์†Œ๋“œ๋“ค๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     


     namedtuple 

    - Tuple ํ˜•ํƒœ๋กœ Data ๊ตฌ์กฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

    - ์ €์žฅ๋˜๋Š” data์˜ variable์„ ์‚ฌ์ „ ์ง€์ •!

     

    from collections import namedtuple

     

    Box = namedtuple('Box', ['width','height'])
    b = Box(20,40)
    w,h = b
    print(w, h)
    >>> 20 40
    
    print(b.width, b.height) # ๋ณ€์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    >>> 20 40
    
    print(b)
    >>> Box(width=20, height=40)
    
    #(class์™€ ๋น„์Šท)

     

    ๋Œ“๊ธ€