본문 바로가기

Programming/python36

python, property decorator 을 이용한 get/set 구현 파이썬 클래스는 _ (underscore, 밑줄)을 이용한 변수 naming으로 private 변수, 함수, 클래스를 구현할 수 있다.(완벽한 private는 아님) 1. sinlge underscore : _var_name 의 형태 클래스 내부에서 사용되는 형태지만, 해당 모듈을 import로 불러올때는 제외된다. (weak internal use indicator) 클래스 외부에서 직접 접근이 가능하다. 2. double underscore : __var_name 의 형태 클래스 내부에서 사용되는 형태. 외부에서 직접 접근이 불가능 하다. private처럼 동작하는데 완벽한 private는 아니다. (외부에서 접근하는 방법이 존재) class Movie: def __init__(self, movie_.. 2019. 9. 9.
python, networkx, 에서 graph의 layout 종류 pos = nx.spring_layout(G) 이외에도 spectral_layout shell_layout fruchterman_reingold_layout kamada_kawai_layout random_layout 이 있다. 2019. 9. 5.
python, print 시 문자열, 숫자, 자리수 조절하기 strs =[ 'abc','abcd','ab'] nums = [12,1342, 0.241, 2141.5312] for i in range(3): print('%s %f'%(strs[i],nums[i])) 이렇게 출력하면 이렇게 삐뚤삐뚤하게 정렬된다. 문자열.ljust(n) : n개의 문자공간중 좌측정렬 %10.3f : 10의 출력폭(소수점포함) 과 3개의 소수점정밀도 strs =[ 'abc','abcd','ab'] nums = [12,1342, 0.241, 2141.5312] for i in range(3): print('%s %9.3f'%(strs[i].ljust(5),nums[i])) 이렇게 출력하면 문자열 숫자 모두 줄 맞춰 출력된다. 2019. 8. 29.
python, 상위 n개 value의 index추출 a= np.random.rand(100) topid= sorted(range(len(a)),key= lambda i: a[i])[-10:] a[topid] 100개의 랜덤값 만들고 0,1,2 ... 99 의 index array을 만들어서, sorted함수로 해당 index의 a값에 따라서 정렬후 상위 10개 뽑아 내기 2019. 8. 19.