본문 바로가기

Python

Python - List의 요소 위치 찾기 1-D 배열을 생성하는 방법은 흔히 두 가지, builtin의 list 객체와 numpy의 array 객체 두 형태가 많이 사용된다. 근데 두 방법으로 생성된 배열 객체는 요소 위치를 다르게 찾아야 한다. a = [1.1, 2.2, 3.3, 4.4, 5.5] ### list 객체로 생성 b = numpy.arrary([1.1, 2.2, 3.3, 4.4, 5.5]) ### numpy.array 객체로 생성 print("index of 3.3 in a:", a.index(3.3)) print("where of 3.3 in b:", numpy.where(b==3.3)) 단지 사용 메소드만이 아니라, 활용면의 차이가 큰데, 당연하게도 numpy.where()가 훨씬 유연하다. 예컨대, numpy.where(b 더보기
Python - shutil.make_archive() 나의 단순 반복 작업을 줄이기 위해 Python으로 잡일을 시키기로 했다. 디렉토리 트리 구조 내에 특정 이름의 디렉토리를 찾아 그 안의 파일들을 압축하는 일인데, 어리석게도 입력 인자를 범용화 하고 예외처리까지 하고 그러는 바람에 생각보다 오래 걸렸다. (그래서 아직 원하는 기능을 다 못 만들었다; 변명) 처음엔 간단하게 os.popen() 함수로 7z.exe를 호출해서 그럭저럭 원하는 결과를 얻었는데, 7zip이 -tzip 옵션을 줘도 zip 확장자를 안 달아주는 경우가 가끔 발생했다. 확률적인 건 아니고, PyCharm에서 수행하면 확장자 정상 생성, cmd 콘솔에서 수행하면 확장자 없이 생성, 이런 식이었다. 대충 구글링 해봐선 원인을 찾을 수 없었고, 이게 cmd 프로세스에서 python 프로세.. 더보기
Jupyter Notebook / Anaconda 사용 시 주의점 회사에서 두번째 노트북을 받아 환경 설정 중이다. 가끔 짧은 파이썬 코드를 돌릴 때가 있는데, Pycharm으로 프로젝트를 만들거나 엉뚱한 곳에 파일을 넣지 않으려면 주피터 노트북도 쓸만하다 싶어 설치하고 있다. 설치하고 콘솔 창에서 실행해보니, 익스플로러가 열리기만 하고 내용이 로딩되지 않는다. 창 로그에 수동으로 여는 경로가 나오긴 하는데, 실행 시마다 인스턴스가 새로 만들어지는지 랜덤 숫자가 들어있어 저장해두고 쓰거나 할 수가 없다. 검색해 보니 방법이 있다. https://domini21.tistory.com/23 Jupyter Notebook 시작시 브라우저가 자동실행 되지 않을 때 파이썬 개발도구로 주피터 노트북(Jupyter Notebook)을 많이 사용한다. 자동완성 기능이 빈약하거나 ip.. 더보기
Python - about builtin types 워낙 인간 언어에 가까운 semantic 언어이다 보니, 정확히 어떤 결과일지 오히려 예측이 안 된다. a = 1 print(type(a)) if a == int: print("a == int") if a is int: print("a is int") if a is type(int): print("a is type(int)") if type(a) == type(int): print("type(a) == type(int)") if type(a) is type(int): print("type(a) is type(int)") if type(a) is int: print("type(a) is int") if type(a) == int: print("type(a) == int") 결과는 조금 의외다. type(a.. 더보기
Python - 모듈 import ./caller.py ./module/callee.py 호출관계의 두 파일이 이러한 디렉토리 구조로 위치할 때, callee.py의 클래스 Callee는 caller.py에서 참조하는 방법은 아래와 같다. 1) caller.py와 callee.py가 같은 디렉토리에 있을 경우 #방법1 import callee c = callee.Callee() #방법2 from callee import Callee c = Callee() 2) callee.py가 module 디렉토리에 있을 경우 #방법1 from module import callee c = callee.Callee() #방법2 from module.callee import Callee c = Callee() 추가1) 디렉토리는 파일명과 함께 from 구.. 더보기
Python - numpy arange vs. linspace 거의 유사한 동작이며 목적도 비슷하다. numpy.arange() Values are generated within the half-open interval [`start`, `stop`) numpy.linspace() Returns `num` evenly spaced samples, calculated over the interval [`start`, `stop`]. print("arange:", np.arange(start=0, stop=6, step=1)) print("linspace:", np.linspace(start=0, stop=6, num=7)) 결과: arange: [0 1 2 3 4 5] linspace: [0. 1. 2. 3. 4. 5. 6.] linspace의 num은 '분할점의 개.. 더보기
[Python] List에는 최대 몇 개의 요소를 넣을 수 있을까? # max size of list with incremental integers: 59,635,723 # max size of list with constant value '0': 120,898,753 # max size of list with appending copied sub-lists filled with '0': 462,200,000 (= 4622 * 100000) # max size of list with extending copied sub-lists filled with '0': 166,200,000 (= 1662 * 100000) # max size of list with extending copied sub-lists filled with '0': 123,460,000 (= 12346 .. 더보기