목록Python (2)
Kelly's journey to a coding master
list.append() 시, 여러 요소를 추가하려면? in Python
1. append() : 리스트의 마지막에 인자로 전달된 아이템을 추가 list = [1,2,3] list.append(4) print(list) #[1, 2, 3, 4] list.append([5,6]) print(list) #[1, 2, 3, 4, [5, 6]] 2. extend(): 인자로 전달된 iterable(list, tuple 등) 의 모든 아이템을 리스트에 추가 list = [1,2,3,4] list.extend([5,6]) print(list) #[1, 2, 3, 4, 5, 6] list.extend((7,8,9)) print(list) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
Python
2023. 8. 16. 23:30
조합(combination) 직접 구현해보기 in Python
파이썬에는 from itertools import combinations라는 내장함수가 있지만, 어떻게 작동하는지 알면 시간 복잡도 면에서도, 다른 문제를 풀 때 응용하기에도 좋을 것 같아서 직접 구현해보고자 했다. [방식1] def get_comb(arr, n): combinations = [] if n == 0: return [[]] for i in range(0, len(arr)): front = arr[i] back = arr[i+1:] for c in get_comb(back, n-1): combinations.append([front]+c) return combinations [방식2] def comb(population,num): ans = [] ## 정의된 값인지 확인한다. if num >..
Python
2022. 7. 10. 01:21