python 문자열 찾기, 문자열이 list 에 있는 요소를 포함하는지 체크하기
페이지 정보
본문
## 출처 : https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/
Python | Test if string contains element from list
## Method #1 : Using list comprehension
```
# initializing string
test_string = "There are 2 apples for 4 persons"
# initializing test list
test_list = ['apples', 'oranges']
# printing original string
print("The original string : " + test_string)
# printing original list
print("The original list : " + str(test_list))
# using list comprehension
# checking if string contains list element
res = [ele for ele in test_list if(ele in test_string)]
# print result
print("Does string contain any list element : " + str(bool(res)))
```
## Method #2 : Using any()
```
# initializing string
test_string = "There are 2 apples for 4 persons"
# initializing test list
test_list = ['apples', 'oranges']
# printing original string
print("The original string : " + test_string)
# printing original list
print("The original list : " + str(test_list))
# using list comprehension
# checking if string contains list element
res = any(ele in test_string for ele in test_list)
# print result
print("Does string contain any list element : " + str(res)
```
## 참고
https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/
Python | Test if string contains element from list
## Method #1 : Using list comprehension
```
# initializing string
test_string = "There are 2 apples for 4 persons"
# initializing test list
test_list = ['apples', 'oranges']
# printing original string
print("The original string : " + test_string)
# printing original list
print("The original list : " + str(test_list))
# using list comprehension
# checking if string contains list element
res = [ele for ele in test_list if(ele in test_string)]
# print result
print("Does string contain any list element : " + str(bool(res)))
```
## Method #2 : Using any()
```
# initializing string
test_string = "There are 2 apples for 4 persons"
# initializing test list
test_list = ['apples', 'oranges']
# printing original string
print("The original string : " + test_string)
# printing original list
print("The original list : " + str(test_list))
# using list comprehension
# checking if string contains list element
res = any(ele in test_string for ele in test_list)
# print result
print("Does string contain any list element : " + str(res)
```
## 참고
https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/
추천0
댓글목록
등록된 댓글이 없습니다.