Python
-
순열(permutations)과 조합(combinations)Python/개념학습 2022. 11. 4. 15:03
from itertools import ... product('ABCD', repeat=2) AA, AB, AC, AD, BA, BB, BC, BD, CA, CB, CC, CD, DA, DB, DC, DD permutations('ABCD', 2) AB, AC, AD, BA, BC, BD, CA, CB, CD, DA, DB, DC combinations('ABCD', 2) AB, AC, AD, BC, BD, CD combinations_with_replacement('ABCD', 2) AA, AB, AC, AD, BB, BC, BD, CC, CD, DD 출력은? ''.join(comb) reference: https://docs.python.org/ko/3/library/functional.html 함수..
-
map함수 활용Python/개념학습 2022. 10. 27. 13:29
map(function, iterable) map(함수, 반복 가능한 자료형) : 두 번째 인자로 들어온 반복 가능한 자료형을 첫 번째 인자로 들어온 함수에 하나씩 집어넣어 함수를 수행 : 이점 - for문으로 개별 접근해서 연산 후 list에 하나씩 append를 하는 과정을 대체할 수 있음 : 유의 - map함수의 반환값은 map객체이기 때문에 해당 자료형을 list 또는 튜플로 변환시켜줘야 함. : 예제 # X의 5제곱근을 반환하는 함수 정의 def func_pow(x): return pow(x, 5) res=list(map(func_pow, [1,2, 3])) print(res) # 출력 : [1, 32, 243] : map의 인자로 들어갈 함수가 일회성으로만 쓰이거나 매우 짧을 경우 람다 함수로..
-
백준 #2178번 미로탐색Python/문제풀이 다시 하기 2022. 10. 26. 15:46
from collections import deque n, m=map(int, input().split()) matrix=[list(map(int, input())) for _ in range(n)] # 이동할 방향 정의(상, 하, 좌, 우) dx = [0, 0, -1, 1] dy = [1, -1, 0, 0] def bfs(x, y): q=deque() q.append((x, y)) while q: x, y=q.popleft() #print("x, y", q.popleft()) for i in range(4): nx=x+dx[i] ny=y+dy[i] # 범위 벗어나면 if nx=m: continue #벽이면 if matrix[nx][ny]==0: continue #벽이 아니면 이동 가능 if matrix..
-
collections 모듈 - extend, appendPython/개념학습 2022. 8. 21. 12:04
in collections.deque # 예제3-2. append() vs extend() lst2 = ['a', 'b', 'c', 'd'] lst.extend('ef') # extend() lst2.append('ef') # append() print("lst.extend('ef') >> ", lst) print("lst2.append('ef') >>", lst2) ''' 결과 lst.extend('ef') >> ['a', 'b', 'c', 'd', 'e', 'f'] lst2.append('ef') >> ['a', 'b', 'c', 'd', 'ef'] reference: https://excelsior-cjh.tistory.com/entry/collections-모듈-deque [EXCELSIOR:티스..