Kiểm tra xem các từ trong chuỗi python

Điều này trả về chỉ mục đầu tiên mà tại đó chuỗi con "từ" xuất hiện. Nếu không tìm thấy chuỗi nào, nó sẽ trả về -1

Hãy xem một số ví dụ

Ví dụ về kiểm tra xem một chuỗi có phải là chuỗi con của một chuỗi khác không

Hãy tìm hiểu xem từ “This” có xuất hiện trong một chuỗi không

sentence = "This is just a sentence"

if "This" in sentence:
    print["Found it!"]

đầu ra

Found it!

Sau đó, hãy tìm ra chỉ số mà nó xảy ra

sentence = "This is just a sentence. This is another."

idx = sentence.find["This"]
print["The word 'This' begins at:", idx]

đầu ra

The word 'This' begins at: 0

Như bạn có thể thấy, phương thức find[] chỉ quan tâm đến sự xuất hiện đầu tiên của từ “This” trong câu

Một trường hợp cạnh

Nếu bạn đang sử dụng toán tử in, hãy kiểm tra xem chuỗi chính có phải là Không có. Nếu bạn không làm điều đó, bạn sẽ gặp lỗ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ừ trong danh sách này với danh sách từ đã có sẵn mà chúng tôi muốn kiểm tra

Python3




Found it!
26

Found it!
27
Found it!
28
Found it!
29

 

Found it!
30

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0
Found it!
28
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
4
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____130
Found it!
31
Found it!
32

 

Found it!
33

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____136
Found it!
31
Found it!
38
Found it!
39

 

Found it!
27
Found it!
28
Found it!
32
Found it!
33
Found it!
34

 

Found it!
35
Found it!
28
Found it!
37

Found it!
38
Found it!
39
Found it!
30
Found it!
31

Found it!
32
Found it!
38
Found it!
34
Found it!
30
Found it!
36

Found it!
37
Found it!
38
Found it!
39____128
Found it!
28
Found it!
262

Found it!
263
Found it!
35
Found it!
28
Found it!
266

Found it!
263
Found it!
268

Found it!
38
Found it!
35____128
Found it!
28
Found it!
266
Found it!
274

Found it!
32
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8____49
Found it!
278
Found it!
34

Found it!
280
Found it!
274

Found it!
32
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
8____49
Found it!
285
Found it!
34

đầu ra

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

Sử dụng khả năng hiểu danh sách để kiểm tra xem chuỗi có chứa phần tử từ danh sách hay không

Vấn đề này có thể được giải quyết bằng cách sử dụng khả năng hiểu danh sách, trong trường hợp này, chúng tôi kiểm tra danh sách và cả các phần tử chuỗi nếu chúng tôi có thể tìm thấy kết quả khớp và trả về true, nếu chúng tôi tìm thấy một và false thì không sử dụng điều kiện .
 

Python3




Found it!
287

Found it!
288

Found it!
289

 

Found it!
26

Found it!
27
Found it!
28
Found it!
29

 

Found it!
30

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0
Found it!
28
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
4
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____130
Found it!
31
Found it!
32

 

Found it!
33

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____136
Found it!
31
Found it!
38
Found it!
39

 

Found it!
289

Found it!
288

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
07
Found it!
28
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
09
Found it!
38
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
11
Found it!
30
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0
Found it!
38
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
15
Found it!
30
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
17

 

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

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____421
Found it!
31
Found it!
38
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']
String contains the list element
25
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
26

đầu ra

Found it!
3

Sử dụng any[] để 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 để bạn có thể thực hiện tác vụ này và cũng hiệu quả. Hàm này kiểm tra sự trùng khớp trong chuỗi với sự trùng khớp của từng phần tử trong danh sách.
 

Python3




Found it!
288

Found it!
289

 

Found it!
26

Found it!
27
Found it!
28
Found it!
29

 

Found it!
30

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0
Found it!
28
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
4
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____130
Found it!
31
Found it!
32

 

Found it!
33

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____136
Found it!
31
Found it!
38
Found it!
39

 

Found it!
289

Found it!
288

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
07
Found it!
28
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
58
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
15
Found it!
30
Found it!
27
Found it!
38
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
11
Found it!
30
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
65

 

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

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____421
Found it!
31
Found it!
38
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
72

đầu ra

Found it!
3

Sử dụng phương thức find[] để kiểm tra xem chuỗi có chứa phần tử từ danh sách không

Ở đây chúng tôi đang sử dụng phương thức find[] để kiểm tra sự xuất hiện của từ và nó trả về -1 nếu từ đó không tồn tại trong danh sách

Chủ Đề