일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AI
- judge
- TORCH
- Mac
- Linux
- DB
- CUDA
- Container
- GitLab
- file
- Converting
- git
- Flask
- Laravel
- LLM
- list
- numpy
- Python
- Package
- pandas
- docker
- PostgreSQL
- Database
- enV
- evaluation
- format
- Paper
- pytorch
- Windows
- KAKAO
Archives
- Today
- Total
Daily Develope
[Python] 매칭 (문자열 / 리스트 / 정규식) 본문
문자열 매칭
s = "오늘은 기분이 '매우' 좋습니다."
# 단순 문자열 확인
print("기분이" in s) # True
print("나쁩니다" in s) # False
# 정의된 문자열 리스트 중 일치하는 단어가 있는지 확인
print(any(x in s for x in ["좋습니다", "나쁩니다"])) # True
print(any(x in s for x in ["그저그래요", "나쁩니다"])) # False
# 정의된 문자열 리스트 모두가 일치하는지 확인
print(all(x in s for x in ["오늘은", "좋습니다"])) # True
print(all(x in s for x in ["오늘은", "나쁩니다"])) # False
리스트 매칭
a=["hello", "world", "python"]
b=["hello", "bye", "good"]
# 같은 개체 찾기
y = [x for x in b if x in a] # ['hello']
# 서로 다른 개체 찾기
y = [x for x in a+b if x not in a or x not in b] # ['world', 'python', 'bye', 'good']
정규식(re, Regural Expression)
import re
s = "오늘은 기분이 '매우' 좋습니다."
# '매우'를 찾아서 반환
re.findall("'.+?'", s) # ["'매우'"]
# '매우'가 있는지 확인
re.search("'.+?'", s) # True
'Develope > Python' 카테고리의 다른 글
[Python] config 환경설정 관리 및 사용 (0) | 2022.03.31 |
---|---|
[Python] PCM 음성파일 파형 출력 (with pyplot) (0) | 2022.02.15 |
[Python] PCM 파일 통합 및 WAV로 변환 (0) | 2022.02.15 |
[Python] 로그, 로깅 (logging) (0) | 2022.01.15 |
[Python] Pattern - Singleton (싱글톤) (0) | 2022.01.08 |