Dưới đây là một vài cách thay thế để thực hiện nó, có thể nhanh hơn hoặc phù hợp hơn so với câu trả lời của Kennytm, tùy thuộc vào bối cảnh.
1) Sử dụng biểu thức thông thường:
import re words_re = re.compile("|".join(list_of_words)) if words_re.search('some one long two phrase three'): # do logic you want to perform2) Bạn có thể sử dụng các bộ nếu bạn muốn khớp toàn bộ các từ, ví dụ: Bạn không muốn tìm từ "" trong cụm từ "định lý chúng là lý thuyết":
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuffTất nhiên bạn cũng có thể thực hiện toàn bộ các trận đấu từ với regex bằng mã thông báo "\ b".
Hiệu suất của những điều này và giải pháp của Kenny sẽ phụ thuộc vào một số yếu tố, chẳng hạn như thời gian danh sách từ và chuỗi cụm từ và tần suất chúng thay đổi. Nếu hiệu suất không phải là một vấn đề thì hãy đi cho đơn giản nhất, có lẽ là Kenny.
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 : TrueCá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"
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 0____8 word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 2word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 3word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 5word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 6
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 9 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True1
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True4 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True6Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True7
Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: 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 element0The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element1The 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 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 element7The 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'] Does string contain any list element : True0The 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 : True2The 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 : True5The 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 : True0
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True1The 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'] 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 : True1The 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 : True6 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'] Does string contain any list element : True4The 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 : True0word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6The 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'] Does string contain any list element : True8The 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 : True0word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8test_string 3The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list elementSử 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"
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 0____8 word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 2word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 3word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 5word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 6
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 9 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True1
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True4 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True6Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True7
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 element7The 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'] Does string contain any list element : True0The 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 : True2The 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
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : TrueThe original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5The 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 : True0 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 : True6 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']
Does string contain any list element : True4The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True2
Python3
test_string = "There are 2 apples for 4 persons"
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 0____8 word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 2word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 3word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 5word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 6
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 9 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True1
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True4 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True6Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True7
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 element7The 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'] Does string contain any list element : True0The 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 : True2The 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
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : TrueThe original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5The 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 : 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'] String contains the list element3==The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2
Python3
test_string = "There are 2 apples for 4 persons"
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 0____8 word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 2word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 3word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 5word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 6
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 9 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True1
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 7word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 8Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True4 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True0 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True6Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True7
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 73=word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 75
word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 76=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 element7The 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'] Does string contain any list element : True0The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 85=word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 87The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 89
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 76Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: 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 : True4
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True6word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 96=The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 89
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0word_set = set(list_of_words) phrase_set = set('some one long two phrase three'.split()) if word_set.intersection(phrase_set): # do stuff 73=Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True03
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0The 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 : True2The 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
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True