목록Basic (70)
HappyWeasel
# 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 : 이어 쓰기
startswith() : 문자열 앞의 문자를 확인한다. word = "super" print(word.startswith('s')) split() : 문자열을 나눠서 리스트로 만든다. numbers = " 1 2 3 " print(numbers.split()) print(numbers.split(' ')) append() : 리스트에 새로운 item을 추가해준다. upper() : 대문자 lower() : 소문자
int : 정수 float : 실수 str : 문자열 list : 리스트 ex) var = '10' var = int(var) # 정수로 변경