Contents

Python_Library

โ€‹

Python Library ๐Ÿ˜€

  • Idea

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    # ๋ชจ๋‘ '๊ฑฐ์ง“'์ด ๋“ค์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋Š” '๊ฑฐ์ง“'์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?
    # ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ดค๋‹ค. 
    
    a = [False, False, False]
    
    if a == False:
        print("๊ฑฐ์ง“")
    else:
        print("์˜ค๋ฅ˜")
    
    # >>> ์˜ค๋ฅ˜
    
    # ์•ˆ๋œ๋‹ค. ์ƒ๊ฐ์„ ๋ง์•„๋ผ..ใ…Žใ…Ž
    

    โ€‹

  • raise ์˜ˆ์™ธ(‘์—๋Ÿฌ๋ฉ”์‹œ์ง€’)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    # python์—์„œ๋Š” ์˜๋„์ ์œผ๋กœ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ
    # ์ด๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด raise ๋ฉ”์„œ๋“œ.
    
    try:
        x = 2
        if x % 3 != 0:                               
            raise Exception('3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.')  
        print("์ž…๋ ฅ๋œ ๊ฐ’", x)
    except Exception as e:                      
        print('์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.', e)
    
    >>> ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
    

    โ€‹

  • filter() VS find()

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    ## filter(function, iterable)
    # ๋ฐฐ์—ด์˜ `๋ชจ๋“  ์š”์†Œ`์— ์ ‘๊ทผํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” `๊ฐ’`์„ ์ฐพ๋Š”๋‹ค.
    
    # ex)
    def func(x):
        if x > 0:
          return x
        else:
          return None
    
    list(filter(func, range(-5,10)))
    >>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    # ex)
    [ i for i in range(-5,10) if i > 0 ]
    >>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    
    # find(sub[, start[, end]])
    # ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์ฐพ์œผ๋ ค ํ•˜๋Š” ๊ฐ’์˜ `index`๋ฅผ ๋ฐ˜ํ™˜ ํ•ด์ค€๋‹ค.
    
    # ex)
    a = 'hello'
    a.find('o')  # find ํ•จ์ˆ˜
    >>> 4
    

    โ€‹

  • index() VS find()

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    ## ๊ณตํ†ต์ 
    # ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ์ฐพ์œผ๋ ค ํ•˜๋Š” ๊ฐ’์˜ `index`๋ฅผ ๋ฐ˜ํ™˜ ํ•ด์ค€๋‹ค.
    # ex)
    >>> 'oxoxoxoxox'.find('x')  # find ํ•จ์ˆ˜
    1 
    >>> 'oxoxoxoxox'.index('x')  # index ํ•จ์ˆ˜
    1
    
    # ๋ฌธ์ž 'o'๊ฐ€ ์ฒซ๋ฒˆ์งธ ์œ„์น˜ํ•œ ์ž๋ฆฌ๋ฅผ ์ถœ๋ ฅ
    >>> a = 'hello'
    >>> a.find('o')  # find ํ•จ์ˆ˜
    4
    >>> a.index('o')  # index ํ•จ์ˆ˜
    4
    
    # ๋˜ (value, start, end) ํ˜•ํƒœ๋กœ ๋ฌธ์ž๋ฅผ ์ฐพ๋Š” ์‹œ์ž‘์ ๊ณผ ์ข…๋ฃŒ์ ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    # ๋ฌธ์ž์—ด์ค‘ 2๋ฒˆ์งธ ์œ„์น˜๋ถ€ํ„ฐ ์ฒ˜์Œ 'x'๊ฐ€ ์œ„์น˜ํ•œ ์ž๋ฆฌ
    >>> 'oxoxoxoxox'.index('x', 2)
    3
    
    # a๋ณ€์ˆ˜์—์„œ 1๋ฒˆ์งธ~3๋ฒˆ์งธ ์‚ฌ์ด์— ๋ฌธ์ž 'o'๊ฐ€ ์œ„์น˜ํ•œ ์ž๋ฆฌ
    >>> a = 'hello'
    >>> a.find('o', 1, 3)
    -1	
    # findํ•จ์ˆ˜๋Š” ์ฐพ๋Š” ๊ฐ’์ด ์—†์„ ๋•Œ -1์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๊ฒŒ ์ฐจ์ด์ .
    
    
    ## ์ฐจ์ด์ 
    # ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ find()๋Š” '-1'์„ ๋ฐ˜ํ™˜ / index()๋Š” 'ValueError' ๋ฐ˜ํ™˜
    

    โ€‹

  • deque

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    # deque๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ€ํ˜•(ex. pop() ...)ํ•˜๋ฉฐ ์ฝ”๋”ฉํ•  ๋•Œ
    # ์†๋„๊ฐ€ ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ๋‹ค. 
    # ์ฃผ๋กœ popleft()๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ด ๋” ์žˆ๋‹ค. 
    
    ## 1. rotate
    from collections import deque
    
    q = deque([1,2,3,4,5,6])
    q.rotate(3)
    print(q)	# deque([4, 5, 6, 1, 2, 3])
    
    # ์ฃผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ๋์ด ๋ถ™์–ด ์žˆ๋Š” ์›์ˆœ์—ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํŽธ๋ฆฌํ•˜๋‹ค. 
    # ๋‹ค๋งŒ ์†๋„๊ฐ€ ์–ด๋–ค์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. 
    
    ## 2. deque๋Š” ์Šฌ๋ผ์ด์‹ฑ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
    # ๊ตณ์ด ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•„๋ž˜๋ฅผ ์ฐธ๊ณ 
    deque_slice = collections.deque(itertools.islice(my_deque, 10, 20))
    
    # deque๋Š” 
    # index()๋Š” '์‚ฌ์šฉ๊ฐ€๋Šฅ'ํ•˜๊ณ  find()๋Š” '์‚ฌ์šฉ๋ถˆ๊ฐ€'ํ•˜๋‹ค. 
    

    โ€‹

  • q[len(q)-1] VS q[-1] ์†๋„ ๋น„๊ต

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    import time
    start = time.time()  # ์‹œ์ž‘ ์‹œ๊ฐ„ ์ €์žฅ
    ...
    ...
    print("time :", time.time() - start)  # ํ˜„์žฌ์‹œ๊ฐ - ์‹œ์ž‘์‹œ๊ฐ„ = ์‹คํ–‰ ์‹œ๊ฐ„
    
    # q[len(q)-1] VS q[-1] ์†๋„ ๋น„๊ต
    # q[len(q)-1] ์ด ๋” ๋น ๋ฆ„.
    # ์™œ..?
    

    โ€‹

  • isalpha(), isdigit()

    1
    
    # ์•ŒํŒŒ๋ฒณ์ด๋ƒ?, ์ˆซ์žํ˜•์ด๋ƒ? ๋ฅผ ์ฐธ, ๊ฑฐ์ง“ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
    

    โ€‹

  • sys.setrecursionlimit(์ตœ๋Œ€ ์žฌ๊ท€ ๊นŠ์ด ์„ค์ •)

    1
    2
    3
    
    # ์ตœ๋Œ€ ์žฌ๊ท€ ๊นŠ์ด๋ฅผ ๋Š˜๋ฆฌ๋ ค๋ฉด sys ๋ชจ๋“ˆ์˜ setrecursionlimit ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ
    # (๊ธฐ๋ณธ๊ฐ’์ด์ƒ์œผ๋กœ ์•ˆํ•ด์ฃผ๋ฉด ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.) โ€ป๊ธฐ๋ณธ๊ฐ’:1000 
    sys.setrecursionlimit(50000)
    

    โ€‹

  • sorted()

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    sorted๋Š” ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์˜ ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ
    
    ## ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ์„ ๋น„๊ตํ•ด ์ค€๋‹ค.
    # ex)
    A = [(1,2,1), (1,2,2,3), (1,1,3), (1,1,2)]
    A = sorted(A)
    print(A)    # [(1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2, 3)]
    
    ## ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๋‘๋ฒˆ์งธ ์ธ์ž๋“ค์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
    A = [(0, 4), (2, 2), (1, 2), (1, -1), (3, 3)]
    B = sorted(A, key=lambda L: L[1])
    print(B)	# [(1, -1), (2, 2), (1, 2), (3, 3), (0, 4)]
    
    ## ๋‘๋ฒˆ์งธ ์ธ์ž ์šฐ์„  ๊ธฐ์ค€ ํ›„ ์ฒซ๋ฒˆ์งธ ์ธ์ž ๊ธฐ์ค€ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
    A = [(0, 4), (2, 2), (1, 2), (1, -1), (3, 3)]
    B = sorted(A, key=lambda L: (L[1], L[0]))
    print(B) 	# [(1, -1), (1, 2), (2, 2), (3, 3), (0, 4)]
    
    ## set์ž๋ฃŒํ˜•์„ sorted ํ•˜๋ฉด ์ž๋™์œผ๋กœ list type์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
    a = {"a","b","C"}
    print("a์˜ ํƒ€์ž…: ",type(a))	 # a์˜ ํƒ€์ž…:  <class 'set'>
    b = sorted(a)
    print("b์˜ ํƒ€์ž…: ", type(b))	 # b์˜ ํƒ€์ž…:  <class 'list'>
    

    โ€‹

  • from collections import Counter

    ์ฃผ์–ด์ง€๋Š” ์ปจํ…Œ์ด๋„ˆ(ex ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•, string)์— ๋™์ผํ•œ ๊ฐ’์˜ ๊ฐฏ์ˆ˜๋ฅผ ํŒŒ์•…

    1
    2
    3
    4
    5
    6
    
    from collections import Counter
    MoResult = Counter([7, 1, 2, 5, 1, 8, 7, 6]).most_common()
    print(MoResult)		#[(7, 2), (1, 2), (2, 1), (5, 1), (8, 1), (6, 1)]
    
    # .most_common() ํ•จ์ˆ˜
    # ์ž…๋ ฅ๋œ ์ธ์ž๋“ค์˜ '์ˆœ์„œ'๋ฅผ ์กด์ค‘ํ•˜๋ฉด์„œ, '์ค‘๋ณต countํ•ด์คŒ'๊ณผ ๋™์‹œ์— '์ค‘๋ณต ์‚ญ์ œ'๊นŒ์ง€ ์ง„ํ–‰.
    

    โ€‹

  • import sys

    ๋น ๋ฅธ ์†๋„๋กœ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    imput VS sys.stdin.readline()
    
    # python์˜ ๋Œ€ํ‘œ์ ์ธ ์ž…๋ ฅ ํ•จ์ˆ˜
    imput()
    
    
    # ๋‹จ, ๋ฐฑ์ค€์—๋Š” ๋” ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ด ์ดˆ๊ธฐ์— ์„ค๋ช…๋˜์–ด์žˆ๋‹ค.
    import sys
    sys.stdin.readline()	
    # ๋‹จ ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ ๋ฐ›์€ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰์— ๊ฐœํ–‰ ๋ฌธ์ž๊นŒ์ง€ ์ž…๋ ฅ๋˜์–ด ๋’ค์—
    # ์ถ”๊ฐ€ ํ•จ์ˆ˜(rstrip())๋ฅผ ๋ถ™์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณดํ†ต์ด๋‹ค. 
    

    โ€‹

  • ์ •๊ทœํ‘œํ˜„์‹

    • ์ผ๋Œ€์ผ ๋งค์นญ๋˜๋Š” ๋ฌธ์ž

    ์ •๊ทœํ‘œํ˜„์‹ ์•ˆ์—์„œ, ๋ฐ”๋กœ ๋‹ค์Œ ์ ˆ์—์„œ ์„ค๋ช…ํ•˜๋Š” ๋ฉ”ํƒ€๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž ํ•˜๋‚˜๋Š” ์ผ๋ฐ˜ ๋ฌธ์ž์—ด ํ•˜๋‚˜์™€ ๋งค์นญ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, a๋Š” a์™€ ๋งค์นญ๋˜๊ณ , ๊ฐ€๋Š” โ€˜๊ฐ€โ€™์™€ ๋งค์นญ๋˜๋Š” ์‹์ด๋‹ค. ๋‹น์—ฐํžˆ a๊ฐ€ โ€˜bโ€™ ๋˜๋Š” โ€˜๊ฐ€โ€™์™€ ๋งค์นญ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.

    ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ์ •๊ทœํ‘œํ˜„์‹์ด๋“  ๋ฉ”ํƒ€๋ฌธ์ž(ํŠน์ˆ˜ํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฌธ์ž)๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•œ๋‹ค.

    ํŒŒ์ด์ฌ re ๋ชจ๋“ˆ์˜ ๋ฉ”ํƒ€๋ฌธ์ž๋Š” ์ด 12๊ฐœ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.

    $()*+.?[^{|

    ์ด๋“ค ๋ฉ”ํƒ€๋ฌธ์ž๋Š” ๊ฐ๊ฐ์˜ ๋ฌธ์ž ํ•˜๋‚˜์— ๋งค์นญ๋˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ผ๋ฐ˜ ๋ฌธ์ž์ธ a๋Š” ๋ฌธ์ž โ€˜aโ€™์— ๋งค์นญํ•˜์ง€๋งŒ, ์—ฌ๋Š” ์†Œ๊ด„ํ˜ธ (๋Š” ๋ฌธ์ž โ€˜(โ€˜์™€ ๋งค์นญํ•˜์ง€ ์•Š๋Š”๋‹ค.

    ๊ทธ๋Ÿผ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์— ์†Œ๊ด„ํ˜ธ๊ฐ€ ์žˆ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

    ์œ„์˜ ๋ฌธ์ž๋“ค์˜ ์•ž์— ๋ฐฑ์Šฌ๋ž˜์‹œ \๋ฅผ ๋ถ™์—ฌ ์ฃผ๋ฉด ์ผ๋ฐ˜ ๋ฌธ์ž์ฒ˜๋Ÿผ ํ•œ ๊ธ€์ž์— ๋งค์นญ๋œ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด \(๋Š” ๋ฌธ์ž โ€˜(โ€˜์™€ ๋งค์นญ๋œ๋‹ค.

  • find(์ฐพ์„๋ฌธ์ž, ์ฐพ๊ธฐ์‹œ์ž‘ํ• ์œ„์น˜) - ์ฐพ๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

    1
    2
    3
    4
    
    s = '๊ฐ€๋‚˜๋‹ค๋ผ ๋งˆ๋ฐ”์‚ฌ์•„ ์ž์ฐจ๋งˆํƒ€ ํŒŒํ•˜'
    print(s.find('๋งˆ'))				# s์—์„œ ์ฒซ'๋งˆ'์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
    print(s.find('๋งˆ', 3))			# s[3:]๋ถ€ํ„ฐ ์ฒซ'๋งˆ'์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ (์ „์ฒด ๋ฒ”์œ„ ๊ธฐ์ค€)
    print(s.find('๊ฐ€', 2))			# s[2:]๋ถ€ํ„ฐ ์ฒซ'๊ฐ€'์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ (์—†์œผ๋ฉด -1 ๋ฐ˜ํ™˜)
    
    1
    2
    3
    
    5
    5
    -1
    

    โ€‹

  • startsWith(์ฐพ์„๋ฌธ์ž, ์ฐพ๊ธฐ์‹œ์ž‘์œ„์น˜, ์ฐพ๊ธฐ์ข…๋ฃŒ์œ„์น˜)

    1
    2
    3
    4
    
    str = "this is string";
    print(str.startswith('this'))			# str๋งจ์•ž์ด 'this'๋กœ์‹œ์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
    print(str.startswith('is', 2, 4))		# str[2:4]์—์„œ ๋งจ์•ž์ด 'is'์ธ์ง€ ๊ฒ€์‚ฌ
    print(str.startswith('this', 3, 4))		# str[3:4]์—์„œ ๋งจ์•ž์ด 'this'์ธ์ง€ ๊ฒ€์‚ฌ
    
    1
    2
    3
    
    True
    True
    False
    

    โ€‹

  • import itertools (ํšจ์œจ์ ์ธ ๋ฐ˜๋ณต์„ ์œ„ํ•œ ํ•จ์ˆ˜)

    • permutations(์ž๋ฃŒ, ๋งŒ๋“ค์ˆœ์—ด๊ธธ์ด)

      n๊ฐœ์˜ ์›์†Œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์„œ๋ฅผ ์ •ํ•˜์—ฌ r๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ => ์ˆœ์—ด ๊ณต์‹ : nPr = n!/(n-r)!

      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      import itertools
      
      a = [1,2,3]
      permute = itertools.permutations(a,2)
      print(list(permute))
      
      a = "abc"
      permute = itertools.permutations(a,2)
      print(list(permute))
      
      1
      2
      
      [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
      [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
      

      โ€‹

    • combinations(์กฐํ•ฉ)

      n๊ฐœ์˜ ์›์†Œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์„œ์˜ ๊ด€๊ณ„์—†์ด r๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ => ์กฐํ•ฉ ๊ณต์‹ : nCr=nPr/r!

      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      import itertools
      
      a = [1,2,3]
      permute = itertools.combinations(a, 2)
      print(list(permute))
      
      a = "abc"
      permute = itertools.combinations(a, 2)
      print(list(permute))
      
      1
      2
      
      [(1, 2), (1, 3), (2, 3)]
      [('a', 'b'), ('a', 'c'), ('b', 'c')]