Hướng dẫn python check if list of words in string - python kiểm tra xem danh sách các từ trong chuỗi

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:

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 perform

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":

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff

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

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
2
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
3
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
4
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
5
word_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
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
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
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
8=
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3=
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
5

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The 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
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
7==
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3=
The 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
1
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3==
The 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
2

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2

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 : True
2

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_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 3
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2

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"

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
2
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
3
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
4
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
5
word_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
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
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
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4

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 list

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3==
The 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
2
 

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
2
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
3
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
4
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
5
word_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
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
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
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4

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

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
3==
The 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
2

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
2
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
3
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
4
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
5
word_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
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
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
9
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
1

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
7
word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersection(phrase_set):
    # do stuff
8
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
4
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
6
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
7

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 element
5

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
7
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
9

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
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
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
87
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
word_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 : True
5
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
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
0=
The 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
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
96=
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4
word_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 : True
0
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=
Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True
03

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
0
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
6
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
2
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True
4

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.