HappyWeasel
import csv with open('movies.csv') as file: reader = csv.reader(file, delimiter=',') for row in reader: print(row[0])
집합을 의미한다. 특징 순서가 없다 set = {1,2,3} / set = {3,2,1} : 동일한 집합이다. 중복이 없다 set = {1,2,3,3,4,4,5} / set = {1,2,3,4,5} 로 인식한다. 원소 추가/삭제 num_set = {1,3,5,7} num_set.add(9) # 9를 추가 num_set.update([3,15,4]) # 새로운 원소들로 덮어씌운다 num_set.remove(7) # 7을 삭제 (값이 반듯이 존재해야한다. 오류발생) num_set.discard(13) # 13을 삭제(값이 없으면 무시) 집합 연산 set1 = {1,3,5,7} set2 = {1,3,9,27} union = set1 | set2 #합집합 intersection = set1 & set2 # 교집..
# json 패키지를 임포트합니다. import json #JSON 파일을 읽고 문자열을 딕셔너리로 변환합니다. def create_dict(filename): with open(filename) as file: json_string = file.read() return json.loads(json_string) #JSON 파일을 읽고 딕셔너리를 JSON 형태의 문자열로 변환합니다. def create_json(dictionary, filename): with open(filename, 'w') as file: # 함수를 완성하세요. json_string = json.dumps(dictionary) file.write(json_string) # 아래 주석을 해제하고 결과를 확인해보세요. src = 'ne..
matplotlib Mathematical Plot Libary 파이썬에서 그래프를 그릴 수 있게 하는 라이브러리 꺾은선 그래프, 막대 그래프 등을 모두 지원 # matplotlib의 일부인 pyplot 라이브러리를 불러옵니다. import matplotlib.pyplot as plt # 엘리스에서 차트를 그릴 때 필요한 라이브러리를 불러옵니다. from elice_utils import EliceUtils elice_utils = EliceUtils() # 월별 평균 기온을 선언합니다. 수정하지 마세요. years = [2013, 2014, 2015, 2016, 2017] temperatures = [5, 10, 15, 20, 17] #막대 차트를 출력합니다. def draw_graph(): # 막대 ..
numbers = [-1, 3, -4, 5, 6, 100] #key=abs이면 절대값 기준으로 오름차순 정렬한다. sort_by_abs = sorted(numbers, key=abs) #key를 입력하지 않으면 Defalut로 오르차순 정렬을 한다. sort_nomal = sorted(numbers) 사용자 임의의 함수를 만들어서 key로도 사용이 가능하다. # 단어어 해당 단어의 빈도수를 담은 리스트를 선언합니다. 수정하지 마세요. pairs = [ ('time', 8), ('the', 15), ('turbo', 1), ] #(단어, 빈도수) 쌍으로 이루어진 튜플을 받아, 빈도수를 리턴합니다. def get_freq(pair): return pair[1] #(단어, 빈도수) 꼴 튜플의 리스트를 받아, ..
nums = [1, 2, 3] new_nums = [n + 1 for n in nums] # 아래와 동일 nums = [1, 2, 3] new_nums = [] for n in nums: new_nums.append(n + 1)
파일 읽기 file = open('data.txt') content = File.read() file.close() 위와 동일하지만 읽고 자동으로 닫기 with open('data.txt') as File: content = file.read() 파일 줄 단위로 읽기 contents = [] with open('data.txt') as file: for line in file: content.append(line) 파일의 모드 with open('data.txt', 'w') as file: file.write('Hello') w : 쓰기 a : 이어 쓰기