๋ฌธ์์ด ์ฒ๋ฆฌ๋ ํ์ด์ฌ์ผ๋ก
- find()
- isalpha()
- isalnum()
- for ~ in list, string์ผ๋ก ํ๋์ฉ ๊บผ๋ผ ์ ์๋ค.
- ~ in list, string์ผ๋ก๋ง ํ๋ฉด t/f ๋ฐํํ๋ค.
- string slicingํ ๋, ๋งจ ์์ substring์
s[:1]
๊ณผ ๊ฐ์ด ํ๊ธฐํ๊ณ ๋ค์์ ๋ถํฐ ๋์ ๋๋s[-1:]
๊ณผ ๊ฐ์ด ํ๊ธฐํ๋ค. - ๋น 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
- pop()
- popํ ๋ empty list๊ฐ ๊ฑฑ์ ๋๋ฉด ์์๋ฅผ ํ๋ ๋ฃ์ด๋๋ค ๊ธฐ๋ณธ์ผ๋ก
[0]
- popํ ๋ empty list๊ฐ ๊ฑฑ์ ๋๋ฉด ์์๋ฅผ ํ๋ ๋ฃ์ด๋๋ค ๊ธฐ๋ณธ์ผ๋ก
- append()
- ๋งจ๋ค 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|๊ฑฐ์ง|