Hướng dẫn if list in string python - nếu danh sách trong chuỗi python
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. Show
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
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
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 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
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 : True3The 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 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 : 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
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 : True3The 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 : True8The 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
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 Danh sách Python có thể hoạt động với chuỗi không?Danh sách là một trong những cấu trúc dữ liệu phổ biến nhất trong Python và chúng thường được sử dụng để giữ các chuỗi.they are often used to hold strings.
Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một danh sách các chuỗi?Sử dụng hàm bất kỳ () để kiểm tra xem một chuỗi có chứa phần tử từ danh sách không, ví dụ:Nếu bất kỳ (chuỗi con trong my_str cho chuỗi con trong my_list):.Hàm bất kỳ () sẽ trả về true nếu chuỗi chứa ít nhất một phần tử từ danh sách và sai. to check if a string contains an element from a list, e.g. if any(substring in my_str for substring in my_list): . The any() function will return True if the string contains at least one element from the list and False otherwise. |