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.
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.