본문 바로가기

프로그래밍 언어/python

입 출력 속도 개선 한 줄 입력 from sys import stdin n = int(stdin.readline()) 리스트 입력 _list = list(map(int, stdin.readline().split())) # 한줄 읽어 들이고, 공백을 기준으로 분할 한뒤, int로 맵핑한다. 2차원 배열 입력 arr = [] for i in range(col_len): arr.append(list(map(int, stdint.readline().split()))) # append를 쓰는 것이 arr[i]로 접근 하는 것 보다 조금 빠르다. [Reference] https://breakcoding.tistory.com/109 더보기
[Data Structure] List ▶ 2차원 배열 생성 3가지 방법 1) arr = [ [0] * cols ] * rows : 하나의 [0] * cols를 모든 rows의 index에서 가리키는 형상이다. : 따라서 하나의 열만 수정한다 하더라도, 모든 열이 다 수정된다. : 추천하지 않는 2차원 배열 생성 법 2) 가장 추천하는 방법 arr = [ [0 for _ in range(cols)] for _ in range(rows) ] 혹은 arr = [ [0] * cols for _ in range(rows) ] 3) arr = [] for _ in range(cols): col = [] for _ in range(rows): col.append(0) arr.append(col) ▶ 2차원 배열 생성시 속도 개선 방법 mylist = [.. 더보기
[Data Structure] dictionary ※ 자주쓰는 것 위주로 정리 Key와 Value 출력 # Key와 Value 출력 for key, val in dic.items(): print("key = {key}, value = {value}".format(key=key, value=val)) ... key = alice, value=[1, 2, 3] key = bob, value=20 key = tony, value=15 key = suzy, value=30 ... # Key만 출력 for key in dic.keys(): print(key) ''' alice bob tony suzy ''' # Value만 출력 for val in a.values(): print(val) ''' [1, 2, 3] 20 15 30 ''' Key로 Value 얻기 .. 더보기