본문 바로가기

컴퓨터관련 쪽지16

[python] max() 를 이용해 dictionary 에서 value 의 최대값을 가지는 key 를 뽑아보자 일단 코드를 보자 data = { "key1": {"subkey1": {"value1": "100", "value2": 20}}, "key2": {"subkey1": {"value1": "20", "value2": 10}}, } ## value1 의 값중 최대값은 문자열이므로 "20" 이고 반환은 "key2" 를 한다. max_key1 = max(data, key=(lambda x: data[x]['subkey1']["value1"])) ## value2 의 값 중 최대값은 자연수 20이고 반환은 key1을 한다. max_key2 = max(data, key=(lambda x: data[x]['subkey1']['value2'])) 위 코드에서 max() 함수에 data 라는 이름의 사전을 넣어준다. 이.. 2021. 4. 1.
python list sorted 사용 시 내 마음대로 우선 순위 정하기 기본적으로 python sort 에 대한 문서는 아래를 참고한다. docs.python.org/ko/3/howto/sorting.html 정렬 HOW TO — Python 3.9.2 문서 정렬 HOW TO 저자 Andrew Dalke와 Raymond Hettinger 배포 0.1 파이썬 리스트에는 리스트를 제자리에서(in-place) 수정하는 내장 list.sort() 메서드가 있습니다. 또한, 이터러블로부터 새로운 정렬된 리스트를 만 docs.python.org 이 글에서는 내가 원하는 우선순위를 가지고 python list 를 정렬하는 방법을 설명한다. 알아야 할 것은 sorted() 함수와 key 아규먼트이다. 해결하고자 하는 상황: 다음과 같은 list 가 있다. mylist = [("a",1),.. 2021. 3. 30.
pytest 에서 code coverage 사용: 얼만큼 코드에 대해서 테스트를 하는가 확인. python unit test 를 pytest 로 이용할 때 내가 만든 스크립트에 대해서 test code 들이 모두 다 커버하고 있는지 확인할 필요가 있다. 이때는 pytest-cov 를 사용한다. 참조: Reporting — pytest-cov 2.11.1 documentation Reporting It is possible to generate any combination of the reports for a single test run. The available reports are terminal (with or without missing line numbers shown), HTML, XML and annotated source code. The terminal report without l.. 2021. 3. 25.
pytest 사용시 PYTHONPATH=. 설정 방법 작업환경: Ubuntu 디렉토리 구조 . ├── src │ ├── __init__.py │ └── foo.py └── test └── test_foo.py 위와 같은 구조에서 아래와 같이 pytest 를 하면 바로 에러가 발생한다. $ pytest test/ =============================================================================================== test session starts =============================================================================================== platform linux -- Python 3.8.5, pytest-6... 2021. 3. 25.