๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋Š” ํŒŒ์ด์ฌ์œผ๋กœ

  1. find()
  2. isalpha()
  3. isalnum()
  4. for ~ in list, string์œผ๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  5. ~ in list, string์œผ๋กœ๋งŒ ํ•˜๋ฉด t/f ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  6. string slicingํ•  ๋•Œ, ๋งจ ์•ž์˜ substring์€ s[:1]๊ณผ ๊ฐ™์ด ํ‘œ๊ธฐํ•˜๊ณ  ๋’ค์—์„œ ๋ถ€ํ„ฐ ๋Š์„ ๋•Œ๋Š” s[-1:] ๊ณผ ๊ฐ™์ด ํ‘œ๊ธฐํ•œ๋‹ค.
  7. ๋นˆ Sequence(String / Tuple / List)๋Š” False ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. if not x: ~ = ์—†๋‹ค๋ฉด~

strip

strip() # ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์–‘์ชฝ ๊ณต๋ฐฑ๋งŒ ์ œ๊ฑฐ
strip("{") # ํ•ด๋‹น ๋ฌธ์ž์™€ ๊ฐ™์€ ๊ฒƒ ๋ชจ๋‘ ์–‘์ชฝ์—์„œ ์ œ๊ฑฐ
lstrip("{") # ์™ผ์ชฝ์—์„œ๋งŒ strip ์ˆ˜ํ–‰

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

import re
 
s = 'apple orange:banana,tomato'
 
l = re.split(r'[ ,:]', s)
print(l)
 
'''
['apple', 'orange', 'banana', 'tomato']
 
'''
 

์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ๋‚˜๋ˆ„๊ธฐ

re.split(r'(\D)',expression)

2์ฐจ์› ๋ฆฌ์ŠคํŠธ

array = [[0 for col in range(11)] for row in range(10)]
 
array = [[0]*11 for i in range(10)]
 
array = [[0]*11 ]*10

๋‹จ, ์•„๋ž˜์˜ ๋‘ ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ•ด๊ฒฐ ์ด์™ธ์— ์–ด๋– ํ•œ ๊ฐ์ฒด๋ฅผ ๋„ฃ์„ ๊ฒฝ์šฐ๋ผ๋ฉด ์–•์€ ๋ณต์‚ฌ ๋•Œ๋ฌธ์— ๊ฐ’์ด ํ•œ๊บผ๋ฒˆ์— ๋ณ€ํ•˜๋‹ˆ ์ฃผ์˜ํ•  ๊ฒƒ

๋ฆฌ์ŠคํŠธ method

  1. pop()
    1. popํ•  ๋•Œ empty list๊ฐ€ ๊ฑฑ์ •๋˜๋ฉด ์š”์†Œ๋ฅผ ํ•˜๋‚˜ ๋„ฃ์–ด๋‘”๋‹ค ๊ธฐ๋ณธ์œผ๋กœ [0]
  2. append()
  3. ๋งจ๋’ค li[-1]

์ˆœ์—ด

from itertools import permutations
a = [1,2,3]
permute = permutations(a,2)
print(list(permute))
'''
๊ฒฐ๊ณผ
'''
[(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]

์กฐํ•ฉ

from itertools import combinations
a = [1,2,3]
combi = combinations(a,2)
    
print(list(combi))
'''
๊ฒฐ๊ณผ
'''
[(1,2),(1,3),(2,3)]

Product

๋ฐ์นด๋ฅดํŠธ ๊ณฑ์ด๋ผ๊ณ ๋„ ํ•˜๋Š” cartesian product๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค(DB์˜ join, ๊ด€๊ณ„ ๋Œ€์ˆ˜์˜ product๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค). ์ด ๋ฉ”์†Œ๋“œ์˜ ํŠน์ง•์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

from itertools import product
 
_list = ["012", "abc", "!@#"]
pd = list(product(*_list))
# [('0', 'a', '!'), ('0', 'a', '@'), ('0', 'b', '!'), ('0', 'b', '@'), ('1', 'a', '!'), ('1', 'a', '@'), ('1', 'b', '!'), ('1', 'b', '@')]
from itertools import product
 
l = [(x, -x) for x in [1,2,3, 4, 5]]
list(product(*l))
 
[(1, 2, 3, 4, 5),
 (1, 2, 3, 4, -5),
 (1, 2, 3, -4, 5),
 (1, 2, 3, -4, -5),
 (1, 2, -3, 4, 5),
 (1, 2, -3, 4, -5),
 (1, 2, -3, -4, 5),
 (1, 2, -3, -4, -5),
 (1, -2, 3, 4, 5),
 (1, -2, 3, 4, -5),
 (1, -2, 3, -4, 5),
 (1, -2, 3, -4, -5),
 (1, -2, -3, 4, 5),
 (1, -2, -3, 4, -5),
 (1, -2, -3, -4, 5),
 (1, -2, -3, -4, -5),
 (-1, 2, 3, 4, 5),
 (-1, 2, 3, 4, -5),
 (-1, 2, 3, -4, 5),
 (-1, 2, 3, -4, -5),
 (-1, 2, -3, 4, 5),
 (-1, 2, -3, 4, -5),
 (-1, 2, -3, -4, 5),
 (-1, 2, -3, -4, -5),
 (-1, -2, 3, 4, 5),
 (-1, -2, 3, 4, -5),
 (-1, -2, 3, -4, 5),
 (-1, -2, 3, -4, -5),
 (-1, -2, -3, 4, 5),
 (-1, -2, -3, 4, -5),
 (-1, -2, -3, -4, 5),
 (-1, -2, -3, -4, -5)]

while ๋ฌธ ์‚ฌ์šฉ๋ฒ•

while y in li:
    li.index(y)

์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด index๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•จ

Deepcopy ๋ฐฉ๋ฒ•

b = a[:]
b = copy.deepcopy(a)

Queue

  • ๋ฆฌ์ŠคํŠธ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ
a = [3, 4, 5]
a.pop(0)
a.insert(0, 5)
 
[5, 4, 5]

ํ•˜์ง€๋งŒ ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ธก๋ฉด์—์„œ O(n)์ด๋ฏ€๋กœ ์ •๋ง ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์ž.

  • deque
from collections import deque
 
q = deque([3, 4, 5])
q.append(0) # ์˜ค๋ฅธ์ชฝ ์‚ฝ์ž…
q.appendleft(7) # ์™ผ์ชฝ ์‚ฝ์ž…
q.pop() # ์˜ค๋ฅธ์ชฝ ๋นผ๊ธฐ
q.popleft() # ์™ผ์ชฝ ๋นผ๊ธฐ

์‚ฝ์ž…๊ณผ ๋นผ๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ O(1) ํ•˜์ง€๋งŒ, ์ž„์˜ ์›์†Œ์ ‘๊ทผ์— ์žˆ์–ด์„œ O(n)

๋‚ด๋ฆผ, ์˜ฌ๋ฆผ, ๋ฐ˜์˜ฌ๋ฆผ

import math
math.ceil()
math.floor()
round()

์žฌ๊ท€์—์„œ ๋‹ค์Œ์œผ๋กœ ์ •๋ณด ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•

์žฌ๊ท€์—์„œ ๋‹ค์Œ์œผ๋กœ ์ •๋ณด๋ฅผ ๋ณด๋‚ผ๋•Œ ํŠน์ • ์œ„์น˜ ์ž์ฒด๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ธ์ž๋ฅผ ํ•จ๊ป˜ ๋„˜๊ฒจ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ. ์ด๋Ÿด ๊ฒฝ์šฐ ๋งˆ์Šคํ‚น ๊ธฐ๋ฒ•์„ ์“ฐ๋ฉด ์ข‹๋‹ค. ๋งž๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ 3์€ ์ด์ง„์ˆ˜๋กœ 11์ด๋‹ค. ์ด ์˜๋ฏธ์ž์ฒด๊ฐ€ ์–ด๋–ค ์œ„์น˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ. ๋˜๋‹ค๋ฅธ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 3x3 ๋งคํŠธ๋ฆญ์Šค๊ฐ€ ์žˆ์„ ๋•Œ, ์ด ์ •๋ณด๋ฅผ ์žฌ๊ท€๋ฅผ ํƒ€๋ฉด์„œ ๋„˜์–ด๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” i, j ์œ„์น˜๋ฅผ ๋„˜๊ฒจ์ค˜์•ผ ํ•œ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ 4๋ผ๋Š” ์ˆซ์ž๋ฅผ ๋„˜๊ธฐ๊ณ  4/3 = 1, 1 ์ด๋Ÿฐ์‹์œผ๋กœ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

Reduce

from functools import reduce
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
# = ((((1+2)+3)+4)+5)
 

์šฐ์„  ์ˆœ์œ„ ํ

from queue import PriorityQueue
 
que = PriorityQueue()
que = PriorityQueue(maxsize=8) # ํฌ๊ธฐ ์ •ํ•˜๊ธฐ
 
que.put(4)
que.put(1)
que.put(7)
que.put(3)
 
print(que.get())  # 1
print(que.get())  # 3
print(que.get())  # 4
print(que.get())  # 7
 

๋งŒ์•ฝ ๊ตฌ์กฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋ฉด ๋œ๋‹ค.

que.put((3, 'Apple'))
que.put((1, 'Banana'))
que.put((2, 'Cherry'))
 
print(que.get()[1])  # Banana
print(que.get()[1])  # Cherry
print(que.get()[1])  # Apple

์•ž์— ์ˆซ์ž๊ฐ€ ์˜ค๋Š” ํ˜•ํƒœ๋กœ ํ•œ ๋’ค์— ๊ฐ’์„ ์ฝ์–ด์˜ค์ž.

์ด๊ฒŒ ์†๋„๊ฐ€ ๋Š๋ ค์„œ ์ด๊ฑธ ์“ฐ๋Š” ๊ฒƒ์ด ๋‚ซ๋‹ค.

import heapqh
 
heap = []
heapq.heappush(heap, 50)
heapq.heappush(heap, 10)
heapq.heappush(heap, 20)
 
print(heap)
[10, 50, 20]

์ด๋ฏธ ์ƒ์„ฑํ•œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

heap2 = [50 ,10, 20]
heapq.heapify(heap2)
 
print(heap2)

์ž๋ฃŒ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

result = heapq.heappop(heap)
 
print(result)
print(heap)

์ตœ๋Œ€ ํž™์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŠธ๋ฆญ์ด ํ•„์š”ํ•œ๋ฐ, ์œ„์—์„œ ์ ์€ ๊ฒƒ๊ณผ ๊ฐ™์ด ํŠœํ”Œ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.

heap_items = [1,3,5,7,9]
 
max_heap = []
for item in heap_items:
  heapq.heappush(max_heap, (-item, item))
 
print(max_heap)

์ •๋ ฌ

  • ์›์†Œ์˜ ๊ธธ์ด ๋Œ€๋กœ ์ •๋ ฌ
m = '๋‚˜๋Š” ํŒŒ์ด์ฌ์„ ์ž˜ํ•˜๊ณ  ์‹ถ๋‹ค'
m = m.split()
m
['๋‚˜๋Š”', 'ํŒŒ์ด์ฌ์„', '์ž˜ํ•˜๊ณ ', '์‹ถ๋‹ค']
m.sort(key=len)
m
['๋‚˜๋Š”', '์‹ถ๋‹ค', '์ž˜ํ•˜๊ณ ', 'ํŒŒ์ด์ฌ์„']

Dictionary Sort

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์•ฝ๊ฐ„์˜ ํŠธ๋ฆญ์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋ฅผ ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค, sort๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

score_dict = {
    'sam':23,
    'john':30,
    'mathew':29,
    'riti':27,
    'aadi':31,
    'sachin':28
}
 
new_dict = sorted(score_dict.items(), key=lambda x:x[1], reverse=True)
print(new_dict)
# [('aadi', 31), ('john', 30), ('mathew', 29), ('sachin', 28), ('riti', 27), ('sam', 23)]

์•„์›ƒํ’‹์ด ํŠœํ”Œ ํ˜•ํƒœ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ๋‹ค์‹œ dictionary๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด(์‚ฌ์‹ค ๊ทธ๋Ÿด์ผ์€ ์—†๋‹ค) ๋‹ค์‹œ dict ์ž๋ฃŒํ˜• ์•ˆ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

collection

import collections
import itertools
 
def solution(orders, course):
    result = []
 
    for course_size in course:
        order_combinations = []
        for order in orders:
            order_combinations += itertools.combinations(sorted(order), course_size)
 
        most_ordered = collections.Counter(order_combinations).most_common()
        result += [ k for k, v in most_ordered if v > 1 and v == most_ordered[0][1] ]
 
    return [ ''.join(v) for v in sorted(result) ]

์ด ๋ฌธ์ œ์—์„œ Counter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

BFS

from collections import deque
def solution(maps):
    answer = 0
    
    d = [[0, 1], [-1, 0], [0, -1], [1, 0]]
    n, m = len(maps), len(maps[0])
    visited = [[-1 for col in range(m)] for row in range(n)]
    
    q = deque()
    q.append([0, 0])
    visited[0][0] = 1
    while q:
        [y, x] = q.popleft()
        for i in range(4):
            ny, nx = y + d[i][0], x + d[i][1]
            if 0 <= ny < n and 0 <= nx < m:
                if maps[ny][nx] == 1 and visited[ny][nx] == -1:
                    visited[ny][nx] = visited[y][x] + 1
                    q.append([ny, nx])
    
    return visited[-1][-1]

lower bound, upper bound

from bisect import bisect_left, bisect_right 
nums = [0,1,2,3,4,5,6,7,8,9] 
n = 5 
print(bisect_left(nums, n)) 
print(bisect_right(nums, n)) 
 
๊ฒฐ๊ณผ๊ฐ’ 
5 -> ์›ํ•˜๋Š” ๊ฐ’์ด์ƒ์˜ ๊ฐ’์˜ ์ฒ˜์Œ ๋“ฑ์žฅ์œ„์น˜ 
6 -> ์›ํ•˜๋Š” ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์˜ index
from bisect import bisect_left, bisect_right 
nums = [4, 5, 5, 5, 5, 5, 5, 5, 5, 6] 
n = 5 
print(bisect_left(nums, n)) 
print(bisect_right(nums, n)) 
 
๊ฒฐ๊ณผ๊ฐ’ 
1 9

๋ฐฑ์ค€ ์ž…๋ ฅ ๋ฐ›๊ธฐ

from sys import stdin
 
M = int(stdin.readline())
m = sorted(map(int, stdin.readline().split()))
N = int(stdin.readline())
n = list(map(int, stdin.readline().split()))

colab์—์„œ๋Š” ์•ˆ๋œ๋‹ค.

any

a = [True,False,True] 
any(a) True

Set

A = set()
 
A.update([1, 2, 3])
A.add(4)
A.remove(4)

์ž๋ฃŒํ˜•์˜ ์ฐธ ๊ฑฐ์ง“

|๊ฐ’|์ฐธ or ๊ฑฐ์ง“| |:โ€”|:-----:------| |โ€œpythonโ€|์ฐธ| |""|๊ฑฐ์ง“| |[1, 2, 3]|์ฐธ| |[]|๊ฑฐ์ง“| |()|๊ฑฐ์ง“| |{}|๊ฑฐ์ง“| |1|์ฐธ| |0|๊ฑฐ์ง“| |None|๊ฑฐ์ง“|