파이썬 python 자료구조 힙 heapq 간단 구현
최소 힙으로 변환 import heapq # 목록을 최소 힙으로 변환 my_list = [4, 2, 1, 3, 5] heapq.heapify(my_list) print(my_list) # 출력: [1, 2, 3, 4, 5] 힙에 요소 삽입 # 힙에 요소 삽입 heapq.heappush(my_list, 0) print(my_list) # 출력: [0, 2, 1, 4, 5, 3] 힙에서 가장 작은 요소 팝 # 힙에서 가장 작은 요소 팝 smallest = heapq.heappop(my_list) print(smallest) # 출력: 0 print(my_list) # 출력: [1, 2, 3, 4, 5] 힙에서 가장 작은 요소 찾기 # 힙에서 가장 작은 요소 찾기 smallest = my_list[0] pri..
2023. 1. 31.
파이썬 python CSV 파일 저장 불러오기 Read, Write 간단 구현
CSV 저장하기 import csv rows = [["Name", "Age", "City"], ["John", 30, "New York"], ["Jane", 25, "London"], ["Jim", 35, "Paris"]] with open("people.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(rows) CSV 불러오기 import csv with open("people.csv", "r") as file: reader = csv.reader(file) for row in reader: print(row)
2023. 1. 30.