| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
- windows forms
- OpenCV
- Python
- error
- nvidia-smi
- pip
- Selenium
- Docker
- paramiko
- 핑거스타일
- pytorch
- ubuntu
- label
- Numpy
- YOLO
- C
- 기타 연주
- pandas
- JSON
- Visual Studio
- SSH
- C#
- 컨테이너
- 오류
- 프로그래머스
- VS Code
- Linux
- 채보
- mysql
- C++
- Today
- Total
목록Python (107)
기계는 거짓말하지 않는다
import pandas as pd df = pd.read_csv("custom_data.csv", encoding="utf-8") # & (and), | (or) print(df[(df["count"] >= 200) & (df["price"] >= 1000)]) # ~ (not) print(df[~(df["price"] >= 500) | ~(df["count"] > 10)]) ''' query를 이용하여 다중 조건을 한 번에 처리할 수 있다. Column에 ` (Backtick)을 사용하는 이유는 Column의 문자열에 특수문자, 띄어쓰기가 포함될 경우 오류가 발생하기 때문이다. ''' query_string = "`count` >= 200 & `price` >= 1000" print(df.query(..
import pandas as pd df = pd.read_csv("custom_data.csv", encoding="utf-8") print(df[df["price"] == 500]) # price의 값이 500 # price의 값이 500 또는 100 print(df[df["price"].isin([500, 100])]) # 조건 print(df[(df["price"] == 500) | (df["price"] == 100)])
Python 3.7 버전부터 기본 Dict도 삽입 순서를 보장한다. 명시적으로는 collections 모듈의 OrderedDict를 사용할 수 있다. Python Ordered Dict 문서 collections — Container datatypes Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.,,... docs.python.org 아래와 같이 사용할 수 있다. from collections import..
입력받거나 작성된 경로가 절대 경로인지 확인하고 상대 경로이면 절대 경로로, 절대 경로이면 상대 경로로 변환한다. import os data_path = "../../Temp/aa.txt" if not os.path.isabs(data_path): # 절대 경로 변환 data_path = os.path.abspath(data_path) print("abs path:", data_path) else: # 상대 경로 변환 data_path = os.path.relpath(data_path) print("relative path:", data_path)
Python에서 추상클래스를 생성하는 방법이다. abc 모듈이 필요하고 추상클래스의 추상메소드가 존재할 때, 객체를 생성하면 예외가 발생한다. 추상클래스를 상속받은 하위 클래스는 추상메소드를 구현하지 않으면 예외가 발생한다. from abc import * class TempAbstractClass(metaclass=ABCMeta): @abstractmethod def show_val(self): pass class TempClass(TempAbstractClass): def __init__(self, n: int, s: str): self.number = n self.string = s def show_val(self): print(f"Values: {self.number}, {self.string}"..
cv2를 이용할 때 imread, imwrite 한글(utf-8) 인식 문제가 있다. imread, imwrite 함수를 아래와 같이 선언하고 사용한다. import os import cv2 import numpy as np def imread(file_path): npArr = np.fromfile(file_path, dtype=np.uint8) return cv2.imdecode(npArr, cv2.IMREAD_COLOR) def imwrite(file_path, img, params=None): try: ext = os.path.splitext(file_path)[1] result, n = cv2.imencode(ext, img, params) if result: with open(file_path..
문자열은 immutable이므로 문자 수정이 불가능하다. List 변경 후 다시 문자열로 변환하는 방법이 있다. temp_str = "ABCDEF" temp_str = list(temp_str) temp_str[0] = "g" temp_str[1] = "h" temp_str = "".join(temp_str) print(f"Change String: {temp_str} (Type: {type(temp_str)})")
리스트 내포(List comprehension)와 조건문을 이용하여 필요한 값만 남긴다. 아래와 같이 활용할 수 있다. name_dict = {i:1 for i in range(0, 10, 2)} print("necessary value:", list(name_dict.keys())) origin_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] print("origin value:", origin_list) origin_list = [i for i in origin_list if i in name_dict] print("removed value:", origin_list)
