본문 바로가기
반응형

python72

[python] 해당 폴더의 하위까지 모두 삭제하는 방법 Remove directory recursive pip로 install 안해도 되는듯. import shutil shutil.rmtree('/path/to/dir/') 아래의 폴더가 있다고 했을 때, 다 지워집니다. /path/to/dir/chapter01 /path/to/dir/chapter02 /path/to/dir/chapter02/001/ /path/to/dir/chapter02/002/ /path/to/dir/chapter02/003/ 이게 가장 간단한듯. 2020. 2. 12.
[python] package 한번에 install 하기 pip freeze requirements.txt python 개발할때 필요한 library 들을 그때마다 pip install 로 설치를 하고나면 git 이나 어떤것이든 공유를 할때 이 library 들을 편하게 설치할 수 있는 방법입니다. 뭔가 pip 로 library 들을 설치하고 나서 프로젝트 root 에서 $ pip freeze > requirements.txt 이렇게 하면 requirements.txt 파일이 생성되고 그 파일 안의 내용은 pip 로 install 했던 library 정보들이 있습니다. asgiref==3.2.3 certifi==2019.11.28 chardet==3.0.4 deprecation==2.0.7 Django==3.0.3 elasticsearch==7.5.1 file.. 2020. 2. 12.
[python3] opencv 설치 pip 설치 python3 가 설치되어있다면 pip3 도 있을겁니다. 커맨드창에서 실행하거나 파이참 하단영역에 terminal 에서 명령어를 입력합니다. pip3 install opencv-python pip3 가 없는 명령어라고 나오면 pip 로 하세요. 결과는 대충~ 아래처럼 나옵니다. (venv) C:\dev\pycharmProjects\my_test_project>pip3 install opencv-python Collecting opencv-python Downloading https://files.pythonhosted.org/packages/5a/af/dabae171f885ec4d9b2fe6aaf74c1d50a8d32106d840b9fb8eb0095a578d/opencv_python-4.2.0.32.. 2020. 2. 7.
[Python] Dictionary Sort (정렬) lambda Dictionary Sort Lambda 저번 글에서 썼던 dictionary 를 그대로~ ㅎ score_dict = { 'sam':23, 'john':30, 'mathew':29, 'riti':27, 'aadi':31, 'sachin':28 } 저번 글엔 filter 거는걸 했었죠. 2019/11/05 - [Python] - [Python] Dictionary Filter dict 의 스코어대로 정렬하는 방법입니다. new_dict = sorted(score_dict.items(), key=lambda x:x[1], reverse=True) print(new_dict) # [('aadi', 31), ('john', 30), ('mathew', 29), ('sachin', 28), ('riti', 27).. 2019. 11. 5.
[Python] Dictionary Filter Dictionary Filter score_dict = { 'sam':23, 'john':30, 'mathew':29, 'riti':27, 'aadi':31, 'sachin':28 } 이런 dictionary 가 있을 때 30점이 넘는 사람만 가지는 dictionary 를 얻고 싶다면, 기본 적으로 for 문을 이용해서 새로운 dictionary 를 만들면 over_30_dict = {} for key in score_dict.keys() : if score_dict.get(key) >= 30 : over_30_dict[key] = score_dict.get(key) 이런식이겠죠? 좀 더 간편한 방법을 써보면, over_30_dict = dict(filter(lambda elem:elem[1]>=30, .. 2019. 11. 5.
[python] Dictionary 의 특정 값의 sum, max, min 값 가져오기 아래 예제에서 Dictionary List 인 myList 에 아래 값들이 있다고 했을 때 gold 의 총 합을 구하고 싶다면 myList = [ {'points': 400, 'gold': 2480}, {'points': 100, 'gold': 610}, {'points': 100, 'gold': 620}, {'points': 100, 'gold': 620} ] sum(item['gold'] for item in myList) sum , max, min 으로 바꾸면 됩니다. 위의 식이 어렵다면. 그냥 리스트 for 문을 사용해서 myList = [ {'points': 400, 'gold': 2480}, {'points': 100, 'gold': 610}, {'points': 100, 'gold': 620.. 2019. 2. 15.
[python] console 에서 Ctrl+C 로 종료 시 에러메시지 안뜨게 하기 파이썬 스크립트를 콘솔에서 실행중에 Ctrl + C 를 누르면KeyboardInterrupt Exception 이 발생합니다. 콘솔창에 오류어쩌고 하는게 보기 싫다면 아래와 같이 try except 문에 해당 에러를 처리해 주면 됩니다. try: while True: print 1except KeyboardInterrupt: print "test" 2019. 2. 15.
[python]list dictionary sort multiple (여러 키로 정렬시키기) List Dictionary sort multiple 딕셔너리 리스트를 여러개의 key 로 정렬 시키기 sorted 와 lambda 식을 사용하여 멀티 정렬이 가능합니다. 아래의 lambda 식에서 e:( -e['point'], e['penalty']) 를 보면, 내림차순은 - 를 붙혀주면 됩니다. 아래 예제는 point 가 높은 순, point가 같다면 penalty 가 낮은 순으로 정렬하는 예제입니다. dicList = [ {'point':90, 'penalty', 60, 'name' : 'kitti' }, {'point':87, 'penalty', 58, 'name' : 'kate' }, {'point':92, 'penalty', 74, 'name' : 'kevin' }, {'point':90, '.. 2019. 2. 14.
python 오늘날짜, 현재시간 now python datetime now 현재날짜, 시간 import datetime now = datetime.datetime.now() print(now) # 2015-04-19 12:11:32.669083 nowDate = now.strftime('%Y-%m-%d') print(nowDate) # 2015-04-19 nowTime = now.strftime('%H:%M:%S') print(nowTime) # 12:11:32 nowDatetime = now.strftime('%Y-%m-%d %H:%M:%S') print(nowDatetime) # 2015-04-19 12:11:32 2019. 2. 13.
python dictionary 에서 max 값 가져오기 (key or value) python Dictionary max 값 가져오기 import operator stats = {'a':1000, 'b':3000, 'c': 100, 'd':3000} max(stats.iteritems(), key=operator.itemgetter(1))[0] >>> 'b' python 3 에서는 max(stats.items(), key=operator.itemgetter(1))[0] >>> 'b' 또는 lambda 식으로. max_key = max(stats, key=lambda k: stats[k]) 2019. 2. 13.
728x90
반응형