본문 바로가기

분류 전체보기267

[Python] change even & odd element in 2d list list comprehension과 map 둘다 같은 결과로 나온다 아무거나 편한대로 사용하면 될듯 코드 길이는 차이가 별로 안난다. a = [[1,2,1,2],[3,4,3,4]] print(a) odd_list = [[k*10 for k in x[::2]] for x in a] even_list = [[k*20 for k in x[1::2]] for x in a] print(odd_list) print(even_list) print() map(lambda x: x ** 2, range(5)) odd_list = [list(map(lambda x:x*10, x[::2])) for x in a] even_list = [list(map(lambda x:x*20, x[1::2])) for x in a] pri.. 2021. 3. 10.
[프로그래머스] 소수 찾기 파이썬 from itertools import permutations import math def check(n): k = math.sqrt(n) if n < 2: return False for i in range(2, int(k)+1): if n % i == 0: return False return True def solution(numbers): answer = [] for k in range(1, len(numbers)+1): perlist = list(map(''.join, permutations(list(numbers), k))) for i in list(set(perlist)): if check(int(i)): answer.append(int(i)) answer = len(set(answer)) re.. 2021. 3. 10.
[Python] Heap import heapq def my_heap_example(L, T): """ 주어진 비커의 리스트를 힙 구조로 변환 """ heapq.heapify(L) result = 0 while len(L) >= 2 : #IndexError 방지 """ 힙에서 최솟값을 가져옴 """ min_ = heapq.heappop(L) if min_ >= T: # 액체의 최솟값이 T보다 크다는 조건 만족(종료) print("-"*40, "\nresult:", result) return result else: # 두 번째로 작은 값 가져와서 합친 값을 힙에 삽입 min_2 = heapq.heappop(L) heapq.heappush(L, min_ + min_2*2) result += 1 print("step{}: [{},{}.. 2021. 3. 10.
[OpenCV] cv2.addWeighted - Image Overlay cv2를 사용해서 여러 이미지를 overlay 하고 싶어서 찾다가 발견한 자료 background = cv2.imread(img_list[0]) overlay = cv2.imread(img_list[1]) added_image = cv2.addWeighted(background,0.5,overlay,0.5,0) plt.imshow(added_image); deep-learning-study.tistory.com/115 2021. 3. 9.
[Github] Github CLI: issue create and close command line으로 issue number 를 사용해서 close and resolve 등등은 가능한거 같은데 issue create은 cli를 따로 사용해야 되는거 같다. github cli 설치 sudo snap install gh 설치 후 To authecticate gh auth login 하면 몇가지 procedure가 존재하는데 What account do you want to log into? -> Github.com What is your preferred protocol for Git operations? -> HTTPS (관련링크: gist.github.com/grawity/4392747) Authenticate Git with your GitHub credentials? -> .. 2021. 3. 9.
[파이썬을 파이썬답게] 파일 입출력 간단하게 하기 EOF (end of file) 을 체크할 필요가 없고 close할 필요도 없다. 이미 사용하고 있긴 하지만 다시 체크 2021. 3. 8.
[파이썬을 파이썬답게] 가장 큰 수, inf 설정하기 max_value = float('inf') min_values = float('-inf') 파이토치 save criterion 설정할때 9999999, -99999 대신에 쓰면 될듯 2021. 3. 8.
[파이썬을 파이썬답게] class의 자동 string casting class Coord(object): def __init__(self, x, y): self.x, self.y = x, y point = Coord(1, 2) print( '({}, {})'.format(point.x, point.y) ) # 또는 def print_coord(coord): print( '({}, {})'.format(coord.x, coord.y) ) print_coord(point) Using __str__ 메서드 class Coord(object): def __init__ (self, x, y): self.x, self.y = x, y def __str__ (self): return '({}, {})'.format(self.x, self.y) point = Coord(1, 2) 2021. 3. 8.
[파이썬을 파이썬답게] binary-search (bisect) 2021. 3. 8.