본문 바로가기

list9

False - 거짓 python에서 다음은 False에 해당한다 bool False object None int 0 str "" list [] duple () dict {} >>> this_false = None or 0 or '' or [] or () or {} or False >>> this_false False 2020. 9. 28.
Reverse list - 리스트 역순으로 list의 reverse 방법은 list.reverse(), reversed(list), list[::-1], [list.pop() for _ in list] 가 있다 더 있으면 리플달아주세요. 속도는 아래 순이다 list.reverse() < list[::-1] < reversed(list) < [list.pop() for _ in list] l=list(range(2000000)) fidx=1999999 @print_method def use_reverse(): tl = l[:] tl.reverse() print(tl[fidx]) @print_method def use_reversed(): # list를 새로 만든다 tl = l[:] tl = list(reversed(tl)) print(tl[fidx.. 2020. 9. 16.
list method 성능 측정 list 를 만들 때, +, append, comprehension 등을 사용한다 각각의 성능측정 결과, 상황에 맞게 잘 사용하면 좋을 것 같다 le = 1000000 @print_method def test_concat(): # O(k) l=[] for i in range(le): l+=[i] @print_method def test_append(): # O(1) l=[] for i in range(le): l.append(i) @print_method def test_comprehension(): l=[i for i in range(le)] @print_method def test_range(): l=list(range(le)) test_concat() test_append() test_compreh.. 2020. 9. 15.
Index(), bisect() - list 원소의 index값 찾기 list의 index값을 찾는 방법 index를 찾는 list의 크기가 크거나 loop로 찾는다면 bisect() 이진분할 알고리즘을 사용하면 시간을 단축할 수 있다 list.index(n) # list index함수 bisect(list, n) # 이진분할 알고리즘 rmax = 2000000 l = [i for i in range(rmax)] loop = 50 find_n = rmax-2 @print_time def m_index(): for i in range(loop): l.index(find_n) import bisect @print_time def m_bisect(): for i in range(loop): idx = bisect.bisect_left(l, find_n) m_index() m_b.. 2020. 9. 14.
list[::], tuple[::] - extended slices python에서 list, tuple를 자를 때, 효과적인 방법 list[start index:end index+1:step] >>> list_int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # slice >>> list_int[:2] [0, 1] >>> list_int[0:2] [0, 1] >>> list_int[:5] [0, 1, 2, 3, 4] >>> list_int[2:5] [2, 3, 4] >>> list_int[::1] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # even >>> list_int[::2] [0, 2, 4, 6, 8] # odd >>> list_int[1::2] [1, 3, 5, 7, 9] >>> list_int[::3] [0, 3, 6, 9] #.. 2020. 9. 2.
index(), find() - 원소의 인덱스 index(), find() 메소드는 원소의 인덱스 위치를 반환한다 index()는 해당 원소가 없으면 ValueError를 리턴하고, find()는 -1을 리턴한다 find()는 문자열에서만 지원한다 # tuple >>> (1,2,3,4).index(2) 1 >>> (1,2,3,4).index(5) Traceback (most recent call last): File "", line 1, in ValueError: tuple.index(x): x not in tuple >>> (1,2,3,4).find(2) Traceback (most recent call last): File "", line 1, in AttributeError: 'tuple' object has no attribute 'find'.. 2020. 9. 1.
copy - shallow, deep : 깊은복사, 얕은복사 list >>> num_list = [1,2,3,4] # deep copy >>> new_list = num_list[:] >>> new_list2 = list(num_list) set >>> str_set = {'Jane', 'Tim', 'John'} # deep copy >>> new_set = str_set.copy() dict >>> str_dict = {'hi': 'world'} # deep copy >>> new_dict = str_dict.copy() use copy module >>> import copy >>> object = 'other something object' >>> new_obj = copy.copy(object) # shallow copy >>> new_obj2 = copy.. 2020. 8. 19.
문자열 거꾸로(reverse) 시키기 python에서 문자열 거꾸로 출력하기, 4가지 방법 reversed(), list.reverse(), [::-1], deque() # list.reverse(), source list가 바뀜 s = "abcdefg" s_list = list(s) s_list.reverse() print(s_list) ['g', 'f', 'e', 'd', 'c', 'b', 'a'] # reversed(), 뒤집힌 list를 반환함 s = "abcdefg" reversed_s_list = reversed(s) print(reversed_s_list) gfedcba # [::-1], 뒤집힌 list를 반환함 s = "abcdefg" reversed_s_list = s[::-1] print(reversed_s_list) gf.. 2020. 6. 29.
git stash - 작업 임시 저장 git stash 작업 중에 다른 브랜치로 체크아웃하거나 마스터를 리베이스할 경우에 사용하면 좋다 # without message > > > git stash > > > git stash save # git stash save > > > git stash save update tag > > > git stash list > > > stash@{0}: On branch\_name: update tag > > > stash@{1}: WIP on branch\_name: 49bf139 replace parenthesis # 0: git stash save update tag 결과 # 1: git stash # commit 한거, commit 안한거 모두 commit 안한거로 unstash > > > git s.. 2020. 6. 16.