본문 바로가기

공부141

re 모듈 정규식 import re a = re.findall('\d', 'abc123def56zz') print(a) # ['1', '2', '3', '5', '6'] b = re.findall('\d+', 'abc123def56zz') print(b) # ['123', '56'] # match fn = 'abc123def56zz' c = re.match(r'([a-z-. ]+)(\d{,5})(.*)',fn).groups() print(c) # ('abc', '123', 'def56zz') files= ['abc123def56zz'] file = 'abc123def56zz' a = sorted(files, key=lambda file : int(re.findall('\d+', file)[0])) # 첫 숫자로 정렬 # .. 2021. 4. 12.
sys.stdin.readline().rstrip() 한 줄 입력받아 출력하는 코드 import sys input_data = sys.stdin.readline().rstrip() print(input_data) 2021. 4. 10.
BFS from collections import deque # BFS def bfs(graph, start, visited): queue = deque([start]) visited[start] = True while queue: v = queue.popleft() print(v, end=' ') for i in graph[v]: if visited[i] == False: visited[i] = True queue.append(i) graph = [ [], [2, 3, 8], [1, 7], [1, 4, 5], [3, 5], [3, 4], [7], [2, 6, 8], [1, 7], ] visited = [False] * 9 bfs(graph, 1, visited) 2021. 4. 9.
DFS # DFS def dfs(graph, v, visited): visited[v] = True print(v, end=' ') for i in graph[v]: if visited[i] == False: dfs(graph, i, visited) graph = [ [], [2, 3, 8], [1, 7], [1, 4, 5], [3, 5], [3, 4], [7], [2, 6, 8], [1, 7], ] visited = [False] * 9 dfs(graph, 1, visited) 2021. 4. 9.
stack queue 스택(리스트 사용) : append / pop 큐(deque 사용) : append / leftpop -> from collections import deque queue = deque() 2021. 4. 9.
lambda 정렬 c = [(0, 1), (1, 2), (3, 0), (5, 1), (5, 2)] print(c) c.sort(key=lambda x : -x[1]) print(c) 2021. 4. 8.