기계는 거짓말하지 않는다

Python String에 여러 요소가 있는지 판별 본문

Python

Python String에 여러 요소가 있는지 판별

KillinTime 2021. 8. 28. 16:09

in을 사용해서 하나의 요소가 있는지 판별 가능하다.

ex) if e in string:

여러 요소를 한꺼번에 판별하고 싶을 때 any와 for을 함께 사용한다.

# 판별하고 싶은 요소
element = ["One", "Two", "Three", "Four"]
# 요소들이 들어있거나 들어있지 않은 string
string_list = ["abcdOneqwerThreeABCD", "cvbnmtyui", "ertyui", "NNNNNFour"]

for string in string_list:
    if any(e in string for e in element):
        print(string)

결과

 

'Python' 카테고리의 다른 글

Python 예외 처리(Exception)  (0) 2021.09.05
Python DataFrame 특정 값 추출  (0) 2021.09.04
Python argparse module  (0) 2021.08.28
Python 파일 이름 변경  (0) 2021.08.19
Python Numpy any Filter  (0) 2021.08.15
Comments