파이썬 102

[Python] reduce 함수

functools.reduce(function, iterable, initializer) : 왼쪽에서 오른쪽으로 반복을 감소시키면서 함수 연산 왼쪽에서 오른쪽으로 순회를 하게 되고, x는 왼쪽, y는 오른쪽에 할당이 된다. * python3에서는 functools 모듈을 사용해야 한다. reduce를 이용한 모슨 숫자 요소들 더하기 from functools import reduce reduce(lambda x, y: x + y, [2, 4, 6, 8, 10, 12, 14]) ------------------------------------------------------ 56 reduce를 이용한 모슨 숫자 요소들 더하기. (initializer - 초기값 100 고정) from functools im..

[Python] Windows OS Disk Usage Check. (윈도우 운영체제 디스크 사용량 체크)

import shutil.disk_usage(drive) : 드라이브의 전체용량, 사용량, 잔여량 정보를 리턴 import shutil drive = 'c:' c = dict([x for x in zip(['total','used','free'], shutil.disk_usage(drive))]) print(c) print(drive,'Partition used %d %%' %(c['used']/c['total']*100)) ----------------------------------------------------------------------------- {'total': 255005159424, 'used': 224621703168, 'free': 30383456256} c: Partition ..

[Python] Extract directory and filename. (디렉토리와 파일명 추출하기)

os.path.split : 디렉토리 경로와 파일명을 튜플로 리턴 os.path.dirname : 디렉토리 경로를 리턴 os.path.basename : 파일명을 리턴 경로와 파일명이 모두 필요하면 split 을 사용하면 되고, 각각의 정보가 필요하면 dirname과 basename을 쓰면 된다 os.path.split from os.path import split filename = 'C:\\test\\text.txt' os.path.split(filename) ------------------------------------------ ('C:\\test', 'text.txt') os.path.dirname from os.path import dirname filename = 'C:\\test\\te..

[Python] Directory make and remove with subdirectories. (하위 디렉토리 포함 생성과 삭제)

makedirs : 하위 디렉토리를 포함하여 생성 removedirs : 하위 디렉토리를 포함하여 삭제. (파일이 존재하면 [WinError 145] 에러 발생) from os import makedirs from os import removedirs from os.path import isdir dirname = 'test/test' def dircheck(): res = 'Directory Exist' if isdir(dir) else 'Directory Not Exist' return res makedirs(dirname) # make with subdirectories print(dircheck()) removedirs(dirname) # remove with subdirectories print..

[Python] Directory make and remove. (디렉토리 생성과 삭제)

mkdir : 디렉토리를 생성. (하위 디렉토리까지는 생성이 않된다) rmdir : 디렉토리를 삭제. (하위 디렉토리가 존재하면 [WinError 145] 에러 발생) from os import mkdir from os import rmdir from os.path import isdir dirname = 'test' def dircheck(): res = 'Directory Exist' if isdir(dir) else 'Directory Not Exist' return res mkdir(dirname) # create dir print(dircheck()) rmdir(dirname) # remove dir print(dircheck()) -----------------------------------..

[Python] index 함수 - 배열에서 원하는 값의 위치 찾기

index 함수는 배열에서 값의 위치를 찾아주는 함수리며, 중복된 값이 있으면 가장 최소의 위치를 리턴 a 리스트에서 10의 위치 찾기. (최소값인 1이 출력) a = [11,10,12,13,20,31,11,10,10,11] print(a.index(10)) ----------------------------------- 1 a 리스트에서 2번째 ~ 9번째 위치에서 10의 위치 찾기. (최소값인 7이 출력) a = [11,10,12,13,20,31,11,10,10,11] print(a.index(10,2,9)) # index(value, start, end) ---------------------------------------------------- 7 a 문자열에 '1' 이라는 문자 위치 찾기 a =..

[Python] isalnum 문자열에 숫자 또는 알파벳만 있는지 확인하는 메소드

isalnum() : 문자열이 숫자 또는 문자이면 True를 반환 문자열이 숫자로만 구성시 True 리턴 '12345'.isalnum() ----------------- True 문자열이 알파벳로만 구성시 True 리턴 'abcde'.isalnum() ----------------- True 문자열이 한글로만 구성시 True 리턴 '한글'.isalnum() ---------------- True 문자열이 알파벳과 숫자 구성시 True 리턴 'abcde12345'.isalnum() ---------------------- True 문자열이 알파벳과 특수문자 구성시 False 리턴 'abcde@'.isalnum() ------------------ False 문자열이 알파벳과 공백 구성시 False 리턴 '..

[Python] isdigit - Decide if only numbers exist. (문자열에 숫자만 있는지 확인하는 메소드)

isdigit() : 문자열이 숫자로만 구성되어 있으며, 빈칸이 없으면 True를 반환 문자열이 숫자로만 구성시 True 리턴 '12345'.isdigit() ------------------ True 문자열이 숫자와 문자로 구성시 False 리턴 '12345abc한글'.isdigit() ------------------------ False 문자열이 숫자와 특수기호로 구성시 False 리턴 '12345@'.isdigit() ------------------ False 문자열이 숫자와 공백 구성시 False 리턴 '12345 '.isdigit() ------------------ False 활용. (모든 리스트의 요소들이 숫자인지 확인) list_A = [ 1, 12, 43, 4, 15 ] ''.join..