본문 바로가기

공부/Python9

정리 1 조합 from itertools import combinations ( fiic ) arr = ['A', 'B', 'C', 'D'] list(combinations(arr, 2)) #['AB', 'AC', 'AD', 'BC', 'BD', 'CD'] 순열 from itertools import permutations list(permutations(arr, 2)) #['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC'] 디렉토리 a = {} 키 → list(a.keys()) 값 → list(a.values()) 정렬 → a = sorted(a.items(), key=lambda x : x[1]) 정렬 2개 기준 array.sort(.. 2021. 6. 24.
딕셔너리 정렬 fail = {} # value값 기준 정렬 fail = sorted(fail.items(), key=lambda x: x[1]) fail = sorted(fail.items(), key=lambda x: x[1], reverse=True) 2021. 4. 18.
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.
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.
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.