Chúng tôi được cung cấp một chuỗi và nhiệm vụ của chúng tôi là kiểm tra xem chuỗi có chứa các phần tử từ danh sách không.
Example:
Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True
Cách tiếp cận ngây thơ kiểm tra từng từ trong chuỗi
Ở đây chúng tôi đang chia chuỗi thành danh sách các từ và sau đó khớp từng từ của danh sách này với danh sách các từ đã hiện tại chúng tôi muốn kiểm tra.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element1
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
=
____________The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1
=
test_string
2The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True9
test_string
4The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1
=
=
test_string
22The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
=
4The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
=
6=
0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
"There are 2 apples for 4 persons"
1The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element
Sử dụng danh sách hiểu & nbsp; để kiểm tra xem chuỗi có chứa phần tử từ danh sách không to check if string contains element from list
Vấn đề này có thể được giải quyết bằng cách sử dụng danh sách hiểu, trong đó, chúng tôi kiểm tra danh sách và cả với các phần tử chuỗi nếu chúng tôi có thể tìm thấy một trận đấu và trả về true, nếu chúng tôi tìm thấy một và sai là không sử dụng các câu lệnh có điều kiện. & NBSP;
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element1
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4 The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5=
____________The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8 to check if
string contains element from list
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1
=
=
test_string
22Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element1
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
3The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4 The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
5=
____________The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1
=
=
test_string
22Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element1
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element71
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element73
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element74
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True7
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element83
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element85
test_string
2The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element87
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True3
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element74
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element8
=
test_string
2The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element94
=
test_string
2The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element87
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element71
=
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True01
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True8
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True