본문 바로가기

전체 글

python:두 리스트에 모두 존재하는 공통값을 반환하기 ( set, & 연산 ) # 두 리스트에 모두 존재하는 공통값을 반환하기 def intersection(a,b): _a, _b = set(a), set(b) return list(_a & _b) # set연산은 교집합 연산이 가능하다 > 그 다음 list 함수를 통해 list 자료형으로 변환한다 # examples intersection([1,2,3], [2,3,4]) 더보기
python:주어진 숫자가 해당 범위 start, end 안에 있는지 확인하기 # 주어진 숫자가 해당 범위 안에 있는지 확인하기 def in_range(n, start, end = 0): return start 더보기
python:list에 중복된 값이 있으면 true, 없으면 false 반환( set 함수 ) #list에 중복된 값이 있으면 true, 없으면 false 반환 def has_duplicates(1st): return len(1st) != len(set(1st)) # examples x = [1,2,3,4] y = [2,3,3,5] has_duplicates(x) # false has_duplicates(y) # true 더보기
python: list의 마지막 값부터 모든 값에 처음 부터 주어진 함수를 실행하세요 ( slice함수 : [::-1] ) # list의 마지막 값부터 모든 값에 처음 부터 주어진 함수를 실행하세요 def for_each(str, fn): # str[::-1] : slicing 방법을 통해, 배열을 역순으로 정렬한다 for el in str[::-1]: fn(el) for_each([1,2,3], print) 더보기
python: 배열에서, 고유한 값을 필터링해라 ( Counter libraray ) # list에서 고유한 값을 필터링해라 from collections import Counter # Counter : 리스트의 모든 값의 개수를 dictionary로 가져올 수 있다 # 이중에서 값의 개수가 1인 것만을 가져온다 def filter_non_unique(ArrayFirst): return [item for item , count in Counter(ArrayFirst).items() if count ==1] print(Counter([1,2,2,3,4,4,5])) print(Counter([1,2,2,3,4,4,5]).items()) for item, count in Counter([1,2,2,3,4,4,5]).items(): print("item", item) print("count", .. 더보기
python: 배열에서, 고유하지 않은 값을 필터링해라 ( from collection import Counter ) # list에서 고유하지 않은 값을 필터링해라 from collections import Counter # Counter : 리스트의 모든 값의 개수를 dictionary로 가져올 수 있다 # 이중에서 값의 개수가 1인 것만을 가져온다 def filter_non_unique(ArrayFirst): return [item for item , count in Counter(ArrayFirst).items() if count > 1] print(Counter([1,2,2,3,4,4,5])) print(Counter([1,2,2,3,4,4,5]).items()) for item, count in Counter([1,2,2,3,4,4,5]).items(): print("item", item) print("coun.. 더보기
python: 오른쪽에서부터 n개의 요소가 제거된 list를 만들어라 ( a[-n : ] ) # slice기법 def drop_right(a, n=1): return a[:-n] # -를 사용하면, 배열에서 뒤에서부터 접근할 수 있다 # examples drop_right([1,2,3]) # [1,2] 더보기
python: 왼쪽에서부터 n개의 요소가 제거된 list를 만들어라 ( a[n:] ) 본 글은 왼손코딩의 파이썬을 복습한 글입니다 # 왼쪽에서부터 n개의 요소가 제거된 list를 만들어라 def drop(a, n = 1): return a[n:] # n값이 없을때는 왼쪽에서 한개만 제거되는 default원리를 적용한다 # examples drop([1,2,3]) # [2,3] 더보기