목록Python (48)
코딩하는 해맑은 거북이
해당 글은 아래의 2가지를 다룬다. 1. 순열(Permutation) 2. 조합(Combination) 1. 순열(Permutation) : nPr : 서로 다른 n개에서 r개를 뽑아 순서를 정해서 일렬로 나열하는 것. from itertools import permutations perm = list(permutations('n개 원소를 갖는 리스트', r)) from itertools import permutations data = [1, 2, 3, 4] perm = list(permutations(data, 2)) print(perm) perm = list(permutations(data, 3)) print(perm) [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, ..
해당 글은 array를 list로 변환하는 방법에 대해 알아본다. np.array(리스트) list를 array로 변환시키는 방법으로 np.array를 사용하였다. list1 = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]] array1 = np.array(list1) print(array1) [[0 1 2 3 4] [5 6 7 8 9]] ndarray.tolist() 반대로 array를 list로 변환시키는 방법으로 tolist() 함수를 이용한다. array2 = np.arange(10).reshape(2,5) list2 = array2.tolist() print(list2) [[0 1 2 3 4] [5 6 7 8 9]]
해당 글은 filter 함수를 다룬다. - filter(function, iterable) filter 함수는 말그대로 걸러주는 기능을 한다. 즉, 원하는 조건에 맞는 항목만 추출할 수 있다. function에 람다표현식으로 조건을 줄 수 있다. 아래는 홀수, 짝수를 뽑아낸 리스트 모습이다. list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] oddlist = list(filter(lambda x:x%2 != 0, list1)) print(oddlist) evenlist = list(filter(lambda x:x%2 == 0, list1)) print(evenlist) [1, 3, 5, 7, 9] [2, 4, 6, 8, 10] 문자와 정수가 섞여있는 리스트에서 정수형 데이터만 추출한..
해당 글은 reduce 함수를 다룬다. - reduce(function, iterable) reduce 함수는 iterable한 데이터를 결과값을 누적해서 연산해준다. 먼저 reduce 함수를 사용하기 위해선 아래의 모듈을 불러와준다. from functools import reduce list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum = reduce(lambda x,y:x+y, list1) print(sum) 55 (1) 1+2 = 3 (2) 3+3 = 6 (3) 6+4 = 10 ... (9) 45+10 = 55 와 같이 결과값이 누적되어 계산이 된다. list2 = 'abcde' sum = reduce(lambda x,y:y+x, list2) print(sum) edcba..
해당 글은 map 함수를 통해 문자열 리스트를 정수형으로 변환하는 것을 다룬다. - map(function, iterable) map은 iterable한 데이터에 함수를 적용해주는 함수이다. list1 = ['1', '2', '3', '4'] print(list1) list2 = list(map(int, list1)) print(list2) ['1', '2', '3', '4'] [1, 2, 3, 4] function에 람다표현식으로 제곱해주는 함수를 넣어 사용할 수도 있다. list3 = list(map(lambda x:x*x, list2)) print(list3) [1, 4, 9, 16]
본 게시물의 내용은 'Numpy(부스트캠프 AI Tech)' 강의를 듣고 작성하였다. comparisons - All & Any : Array의 데이터 전부(and) 또는 일부(or)가 조건에 만족 여부 반환 a = np.arange(10) a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) a 5), np.all(a 5), np.any(a < 0) # 하나라도 조건에 만족한다면 True (True, False) * numpy는 배열의..