
from collections import deque
# ์์ชฝ ๋์์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ณ ์ ๊ฑฐํ ์ ์๋ deque ์์
d = deque(["a", "b", "c"])
d.append("d") # ์ค๋ฅธ์ชฝ์ ์ถ๊ฐ
d.appendleft("z") # ์ผ์ชฝ์ ์ถ๊ฐ
print("Deque:", d) # ๊ฒฐ๊ณผ: deque(['z', 'a', 'b', 'c', 'd'])
d.pop() # ์ค๋ฅธ์ชฝ์์ ์ ๊ฑฐ
d.popleft() # ์ผ์ชฝ์์ ์ ๊ฑฐ
print("Modified deque:", d) # ๊ฒฐ๊ณผ: deque(['a', 'b', 'c'])from collections import Counter
# ๋ฌธ์์ด์์ ๊ฐ ๊ธ์์ ๋ฑ์ฅ ํ์๋ฅผ ์ธ๊ธฐ
text = "apple"
count = Counter(text)
print("Letter count:", count) # ๊ฒฐ๊ณผ: Counter({'p': 2, 'a': 1, 'l': 1, 'e': 1})from collections import defaultdict
# ๊ธฐ๋ณธ ๊ฐ์ด 0์ธ ๋์
๋๋ฆฌ ์์ฑ
dd = defaultdict(int)
dd["apple"] += 1
dd["banana"] += 2
print("defaultdict:", dict(dd)) # ๊ฒฐ๊ณผ: {'apple': 1, 'banana': 2}import heapq
# ๋ฆฌ์คํธ๋ฅผ ์ต์ ํ์ผ๋ก ๋ณํ
heap = [5, 1, 3, 7, 2]
heapq.heapify(heap)
print("Heapified list:", heap) # ๊ฒฐ๊ณผ: [1, 2, 3, 7, 5]
# ๊ฐ์ฅ ์์ ๊ฐ์ ๊บผ๋ด๊ธฐ
smallest = heapq.heappop(heap)
print("Smallest element:", smallest) # ๊ฒฐ๊ณผ: 1
print("Heap after pop:", heap)
# ์๋ก์ด ๊ฐ์ ํ์ ์ถ๊ฐํ๊ธฐ
heapq.heappush(heap, 0)
print("Heap after push:", heap) # ๊ฒฐ๊ณผ: [0, 2, 3, 7, 5]import heapq
# ๋ฆฌ์คํธ๋ฅผ ์ต๋ ํ์ผ๋ก ๋ณํ
heap = [5, 1, 3, 7, 2]
heap = [-x for x in heap]
heapq.heapify(heap)
print("Max heap:", [-x for x in heap]) # ๊ฒฐ๊ณผ: [7, 5, 3, 1, 2]
# ๊ฐ์ฅ ํฐ ๊ฐ์ ๊บผ๋ด๊ธฐ
largest = -heapq.heappop(heap)
print("Largest element:", largest) # ๊ฒฐ๊ณผ: 7
print("Heap after pop:", [-x for x in heap])
# ์๋ก์ด ๊ฐ์ ํ์ ์ถ๊ฐํ๊ธฐ
heapq.heappush(heap, -4)
print("Heap after push:", [-x for x in heap]) # ๊ฒฐ๊ณผ: [5, 4, 3, 1, 2]import bisect
# ์ด๋ฏธ ์ ๋ ฌ๋ ๋ฆฌ์คํธ
sorted_list = [1, 3, 4, 7, 9]
# ๊ฐ 5 ๊ฐ ๋ค์ด๊ฐ ์ ์ ํ ์์น ์ฐพ๊ธฐ
position = bisect.bisect_left(sorted_list, 5)
print("Insertion point for 5:", position) # ๊ฒฐ๊ณผ: 3
# ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ๋ฉด ์ ๋ ฌ๋ ์ํ ์ ์งํ๊ธฐ
bisect.insort_left(sorted_list, 5)
print("List after insertion:", sorted_list) # ๊ฒฐ๊ณผ: [1, 3, 4, 5, 7, 9]import itertools
list1 = [1, 2, 3]
# ์กฐํฉ(Combination)
combinations = itertools.combinations(list1, 2) # 2๊ฐ์ ์์๋ก ์ด๋ฃจ์ด์ง ๋ชจ๋ ์กฐํฉ
print("Combinations:", list(combinations)) # ๊ฒฐ๊ณผ: [(1, 2), (1, 3), (2, 3)]
# ์์ด(Permutation)
permutations = itertools.permutations(list1, 2) # 2๊ฐ์ ์์๋ก ์ด๋ฃจ์ด์ง ๋ชจ๋ ์์ด
print("Permutations:", list(permutations)) # ๊ฒฐ๊ณผ: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
# ์ค๋ณต ์กฐํฉ(Combination with replacement)
combinations_with_replacement = itertools.combinations_with_replacement(list1, 2) # 2๊ฐ์ ์์๋ก ์ด๋ฃจ์ด์ง ์ค๋ณต ์กฐํฉ
print("Combinations with replacement:", list(combinations_with_replacement)) # ๊ฒฐ๊ณผ: [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
# ์ค๋ณต ์์ด(Permutation with replacement)
permutations_with_replacement = itertools.product(list1, repeat=2) # 2๊ฐ์ ์์๋ก ์ด๋ฃจ์ด์ง ์ค๋ณต ์์ด
print("Permutations with replacement:", list(permutations_with_replacement)) # ๊ฒฐ๊ณผ: [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
# ๋ ๋ฆฌ์คํธ์ ๋ชจ๋ ์กฐํฉ ๊ตฌํ๊ธฐ
list1 = [1, 2]
list2 = ["a", "b"]
combinations = itertools.product(list1, list2)
print("All combinations:", list(combinations)) # ๊ฒฐ๊ณผ: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]