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. Show
1) Sử dụng biểu thức thông thường:
2) 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":
Tấ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 : 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
0____8 2 3 4 5 6 7 8 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 7 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 : True6 Input: 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 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 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'] Does string contain any list element : True0 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 : 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 : True1 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 : True4 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 : 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 : True4 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 : True0 7 8The 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 element2 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 : True2 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0 7 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 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 listVấ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
0____8 2 3 4 5 6 7 8 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 7 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 : True6 Input: 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 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'] Does string contain any list element : True0 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 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 : 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 listThe 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 : True4 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2 Python3
0____8 2 3 4 5 6 7 8 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 7 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 : True6 Input: 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 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'] Does string contain any list element : True0 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 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 : 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 : True0The 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 : True4 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True2 Python3
0____8 2 3 4 5 6 7 8 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 7 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 : True6 Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True7 73= 75 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 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'] 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 85= 87The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4 89The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True5 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 : True6 96= The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True4 89The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True0 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 : True0 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 Output: The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True Làm thế nào để bạn kiểm tra xem một chuỗi có chứa danh sách các từ trong Python không?Sử dụng bất kỳ () để kiểm tra xem chuỗi có chứa phần tử từ danh sách không. Sử dụng bất kỳ chức năng nào là cách cổ điển nhất mà bạn có thể thực hiện nhiệm vụ này và cũng hiệu quả. Hàm này kiểm tra đối sánh trong chuỗi với khớp của từng phần tử của danh sách. to check if string contains element from list. Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
Làm cách nào để kiểm tra xem một chuỗi có chứa nhiều từ trong Python không?Sử dụng hàm tất cả () để kiểm tra xem nhiều chuỗi có tồn tại trong một chuỗi khác không, ví dụ:Nếu tất cả (chuỗi con trong my_str cho chuỗi con trong list_of_strings):.Hàm tất cả () sẽ trả về true nếu tất cả các chuỗi con tồn tại trong chuỗi và sai. to check if multiple strings exist in another string, e.g. if all(substring in my_str for substring in list_of_strings): . The all() function will return True if all of the substrings exist in the string and False otherwise.
Làm thế nào để bạn kiểm tra xem danh sách các ký tự đang ở trong một chuỗi python?Cách đơn giản và nhanh nhất để kiểm tra xem một chuỗi có chứa chuỗi con hay không trong Python là toán tử "trong".Toán tử này trả về true nếu chuỗi chứa các ký tự, nếu không, nó sẽ trả về sai."in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
Làm thế nào để bạn kiểm tra xem một danh sách các chuỗi có trong một chuỗi không?Sử dụng bất kỳ () để kiểm tra xem một chuỗi có chứa một phần tử từ danh sách không.Gọi bất kỳ (có thể lặp lại) với biểu thức máy phát được kiểm tra xem có bất kỳ mục nào từ danh sách có trong chuỗi không.Sử dụng phần tử cú pháp trong chuỗi cho phần tử trong danh sách để xây dựng biểu thức máy phát.. Call any(iterable) with iterable as a generator expression that checks if any item from the list is in the string. Use the syntax element in string for element in list to build the generator expression. |