파이썬 합집합, 교집합 코드 (중복제거)
tuple1 = (1,3,2,6,12,5,7,8) tuple2 = (0,5,2,9,8,6,17,3) new_tuple = tuple1 + tuple2 print(new_tuple) # set을 해줌으로 인해서 자동 내림차순 정렬에 중복제거 효과가 있는것 같다. new_tuple = set(new_tuple) print(new_tuple) # 합집합 (중복x) new_tuple = tuple(new_tuple) print(new_tuple) common_number =[] # 교집합 for i in tuple1: for a in tuple2: if i == a: common_number.append(a) common_number = set(common_number) common_number = tuple(..
2022. 5. 12.
[파이썬으로 익히는 말랑말랑 알고리즘] comprehension과 반복문
이부분은 확실히 if문 나열해줄땐 보긴했는데, for문을 comprehension 한건 처음봤다. 유용한것 같다. # comprehension 방식 string='ABCDEDE' array = [0 for i in range(len(string))] # 위의 코드는 아래의 코드와 같다. # array = [] # for i in range(len(string)): # array.append(0) print(array) 두번째. 각 숫자를 정렬하는 함수 # 각 숫자를 아래차순으로 정렬하고 각 몇개씩 있는지 세는 문제 # 방법1 내장 함수 이용법 from collections import Counter x = [4,0,4,4,1,8,8,2,2,5,0,6,5,6,0] # sort는 출력시, 보이기만 정렬한것같..
2022. 5. 12.