앎을 경계하기

Programming 67

Python tuple, dictionary, set

Python 리스트, 튜플, 딕셔너리, 셋 list = [] tuple = () dict = {} set = set() Tuple은 리스트와 비슷하지만 값의 수정이 불가능하다는 차이점이 있다. t1 = () t1[0] = 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 1 t1 = () ----> 2 t1[0] = 1 TypeError: 'tuple' object does not support item assignment t1 = (1,2,3) t1[0] # list와 같이 인덱스로 접근 가능하다. 1 #괄호를 생략해도 튜플 사용할..

Programming/Python 2019.12.25

Python 리스트 관련 함수

리스트 관련 함수 append() - 추가 sort() - 정렬 reverse() - 뒤집기 index() - 위치 반환 insert() - 요소 삽입 remove() - 요소 삭제 pop() - 요소 꺼내기 count() - 요소 개수 세기 extend() - 리스트 확장 a = [1,2,3] a.append(4) a [1, 2, 3, 4] a.append([5,6]) a [1, 2, 3, 4, [5, 6]] a.append('sfsf') a [1, 2, 3, 4, [5, 6], 'sfsf'] a = [4,2,3,1] a.sort() a [1, 2, 3, 4] a.sort(reverse=True) a [4, 3, 2, 1] a = ['a','z','df'] a.sort() # 첫 알파벳 순으로 정렬 a..

Programming/Python 2019.12.25

Python 리스트 생성, 인덱싱, 슬라이싱, 수정, 삭제

리스트 표현방법 list = [요소1, 요소2, 요소3, ...] a = [] b = [1,2,3] c = ['life', 'is', 'too', 'short'] d = [1,2,'life','is'] e = list() a, b, c, d, e ([], [1, 2, 3], ['life', 'is', 'too', 'short'], [1, 2, 'life', 'is'], []) a == e # 빈 리스트는 [], list()로 생성할 수 있다. True 리스트 인덱싱, 슬라이싱 list는 문자열과 같이 인덱싱하고 슬라이싱할 수 있다. a = [1,2,3] a[0] 1 a[0]+a[2] 4 a[-1] 3 a = [1,2,3,['a','b','c']] a[0] 1 a[-1] ['a', 'b', 'c'] a[-..

Programming/Python 2019.12.25

Python 문자열 관련 함수

a = "hobby" a.count('b') 2 a = "Python is best choice" a.find('b') #있는 경우, 처음 나온 위치(인덱스) 반환 10 a.find('k') #없는 경우, -1 반환 -1 a = 'life is too short' a.index('t') #있는 경우, 처음 나온 위치(인덱스) 반환 8 a.index('k') #없는 경우, 에러 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 a.index('k') #없는 경우, 에러 ValueError: substring not foun..

Programming/Python 2019.12.25

Graph - Bellman-ford's algorithm

참고 사이트 : https://ratsgo.github.io/data%20structure&algorithm/2017/11/27/bellmanford/ bellman-Ford's algorithm keywords : 최단 경로, 음수 사이클, 그래프 개념 벨만-포드 알고리즘은 Shortest path를 구할 때 graph 내 모든 edge에 대해 edge relaxation을 수행한다. 모든 노드 개수 : |V|-1개 모든 edge에 대해 edge-relaxation을 |V|-1회 수행한다. 방법) 시작 노드 A를 제외한 모든 노드의 value를 inf로 초기화한다. 노드 B에서 노드 C로 가는 경우, edge cost가 2인 경우, B노드 값 + 간선(B,C) = inf이기 때문에 업데이트할 필..