Để kiểm tra xem chuỗi có chứa chuỗi con từ danh sách chuỗi hay không, hãy lặp qua danh sách chuỗi và đối với từng mục trong danh sách, hãy kiểm tra xem mục đó có xuất hiện trong chuỗi đã cho hay không
Trong loại tình huống này, chúng ta có
- một chuỗi nguồn, trong đó chúng ta phải kiểm tra xem có chuỗi con nào không
- một danh sách các chuỗi có các phần tử có thể xuất hiện trong chuỗi nguồn và chúng tôi phải tìm xem có bất kỳ chuỗi nào trong danh sách này xuất hiện trong chuỗi nguồn không
ví dụ 1. Kiểm tra xem Chuỗi có chứa Chuỗi con từ Danh sách không
Trong ví dụ này, chúng tôi sẽ lấy một chuỗi nguồn và một danh sách các chuỗi. Chúng ta sẽ sử dụng vòng lặp for để kiểm tra xem chuỗi từ danh sách có xuất hiện dưới dạng chuỗi con trong chuỗi nguồn hay không
Chương trình Python
source_string = 'a b c d e f'
list_of_strings = ['k', 'm', 'e' ]
for substring in list_of_strings:
if substring in source_string:
print['String contains substring from list.']
break
Chạyđầu ra
String contains substring from list.
Vì mục từ danh sách
String contains substring from list.
24 có mặt dưới dạng chuỗi con trong chuỗi nguồn, trong quá trình thực thi vòng lặp for cho phần tử String contains substring from list.
24, điều kiện trong vòng lặp if trở thành TrueBản tóm tắt
Trong hướng dẫn về Ví dụ Python này, chúng ta đã học cách kiểm tra xem chuỗi có chứa chuỗi con từ danh sách chuỗi hay không, với sự trợ giúp của chương trình ví dụ
Sử dụng khả năng hiểu danh sách để tìm chuỗi chứa chuỗi con trong danh sách, e. g.
String contains substring from list.
26. Việc hiểu danh sách sẽ trả về một danh sách mới chứa tất cả các chuỗi chứa chuỗi con đã chỉ địnhVí dụ đầu tiên sử dụng khả năng hiểu danh sách để lấy tất cả các chuỗi từ danh sách chứa chuỗi con đã chỉ định
Khả năng hiểu danh sách được sử dụng để thực hiện một số thao tác cho mọi phần tử hoặc chọn một tập hợp con các phần tử đáp ứng một điều kiện
Trên mỗi lần lặp, chúng tôi kiểm tra xem mục danh sách hiện tại có chứa chuỗi con đã chỉ định hay không và trả về kết quả
Toán tử in kiểm tra tư cách thành viên. Ví dụ:
String contains substring from list.
27 đánh giá thành String contains substring from list.
28 nếu String contains substring from list.
29 là thành viên của String contains substring from list.
30, nếu không, nó đánh giá thành String contains substring from list.
31Danh sách mới chỉ chứa các chuỗi chứa chuỗi con đã chỉ định
Nếu bạn cần lấy các chuỗi chứa chuỗi con trong danh sách theo cách không phân biệt chữ hoa chữ thường, hãy chuyển đổi cả hai chuỗi thành chữ thường
Khả năng hiểu danh sách trả về tất cả các mục danh sách có chứa chuỗi con đã chỉ định, bỏ qua trường hợp
Bạn cũng có thể chuyển đổi kết quả thành chữ thường nếu cần
các str. phương thức Lower trả về một bản sao của chuỗi với tất cả các ký tự có vỏ được chuyển đổi thành chữ thường
Để thực hiện so sánh không phân biệt chữ hoa chữ thường, cả hai chuỗi phải là chữ thường hoặc chữ hoa
Nếu bạn cần tìm chuỗi đầu tiên trong danh sách có chứa chuỗi con, hãy sử dụng hàm
String contains substring from list.
32 với biểu thức gánBiểu thức gán cho phép chúng ta gán cho các biến trong một biểu thức bằng cú pháp
String contains substring from list.
33Biến
String contains substring from list.
34 lưu trữ chuỗi đầu tiên trong danh sách chứa chuỗi con đã chỉ địnhHàm any lấy một iterable làm đối số và trả về
String contains substring from list.
28 nếu bất kỳ phần tử nào trong iterable là đúngNếu chức năng
String contains substring from list.
270 tìm thấy một mục phù hợp, nó sẽ đoản mạch trả về String contains substring from list.
28Ngoài ra, bạn có thể sử dụng vòng lặp
String contains substring from list.
272 đơn giảnVòng lặp
String contains substring from list.
272 lặp qua danh sách và kiểm tra xem mục hiện tại có chứa chuỗi con đã chỉ định hay khôngKhi mục được tìm thấy, chúng tôi thoát ra khỏi vòng lặp để tránh làm việc không cần thiết
Bạn có thể sử dụng phương pháp tương tự để lấy chuỗi đầu tiên trong danh sách chứa chuỗi con đã chỉ định theo cách không phân biệt chữ hoa chữ thường
Chúng tôi chuyển đổi cả chuỗi của lần lặp hiện tại và chuỗi con mà chúng tôi đang kiểm tra thành chữ thường
Cả hai chuỗi phải là chữ thường hoặc chữ hoa để thực hiện so sánh không phân biệt chữ hoa chữ thường
Bạn chọn cách tiếp cận nào là vấn đề sở thích cá nhân. Tôi sẽ sử dụng khả năng hiểu danh sách nếu tôi cần lấy tất cả các chuỗi trong danh sách có chứa chuỗi con
Nếu tôi chỉ cần lấy chuỗi đầu tiên trong danh sách có chứa chuỗi con, tôi sẽ sử dụng vòng lặp
String contains substring from list.
272 vì nó khá dễ đọc Ở đâ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
String contains substring from list.
41String contains substring from list.
42String contains substring from list.
43 String contains substring from list.
44
String contains substring from list.
45The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0______143
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2____23
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
String contains substring from list.
10 String contains substring from list.
11 String contains substring from list.
12
String contains substring from list.
13The 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
String contains substring from list.
16 String contains substring from list.
11 String contains substring from list.
18String contains substring from list.
19
String contains substring from list.
42String contains substring from list.
43String contains substring from list.
12String contains substring from list.
13String contains substring from list.
14
String contains substring from list.
15String contains substring from list.
43String contains substring from list.
17String contains substring from list.
18 String contains substring from list.
19String contains substring from list.
10 String contains substring from list.
11String contains substring from list.
12String contains substring from list.
18 String contains substring from list.
14String contains substring from list.
10 String contains substring from list.
16String contains substring from list.
17String contains substring from list.
18 String contains substring from list.
19____143String contains substring from list.
43String contains substring from list.
412String contains substring from list.
413String contains substring from list.
15____143String contains substring from list.
416String contains substring from list.
413String contains substring from list.
418String contains substring from list.
18 String contains substring from list.
15String contains substring from list.
43String contains substring from list.
43String contains substring from list.
416String contains substring from list.
424String contains substring from list.
12The 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
String contains substring from list.
428String contains substring from list.
14String contains substring from list.
430String contains substring from list.
424String contains substring from list.
12The 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
String contains substring from list.
435String contains substring from list.
14đầ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
String contains substring from list.
437String contains substring from list.
438String contains substring from list.
439
String contains substring from list.
41String contains substring from list.
42String contains substring from list.
43 String contains substring from list.
44
String contains substring from list.
45The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0______143
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2____23
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
String contains substring from list.
10 String contains substring from list.
11 String contains substring from list.
12
String contains substring from list.
13The 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
String contains substring from list.
16 String contains substring from list.
11 String contains substring from list.
18String contains substring from list.
19
String contains substring from list.
439String contains substring from list.
438The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element07
String contains substring from list.
43 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element09
String contains substring from list.
18 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element11
String contains substring from list.
10 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0
String contains substring from list.
18The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element15
String contains substring from list.
10 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element17
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element18
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 element21
String contains substring from list.
11 String contains substring from list.
18The 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 element25
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element26
đầu ra
String contains substring from list.
1Sử 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
String contains substring from list.
438String contains substring from list.
439
String contains substring from list.
41String contains substring from list.
42String contains substring from list.
43 String contains substring from list.
44
String contains substring from list.
45The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0______143
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2____23
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
String contains substring from list.
10 String contains substring from list.
11 String contains substring from list.
12
String contains substring from list.
13The 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
String contains substring from list.
16 String contains substring from list.
11 String contains substring from list.
18String contains substring from list.
19
String contains substring from list.
439String contains substring from list.
438The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element07
String contains substring from list.
43 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element58
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element15
String contains substring from list.
10 String contains substring from list.
42String contains substring from list.
18 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element11
String contains substring from list.
10 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element65
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element18
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 element21
String contains substring from list.
11 String contains substring from list.
18The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element72
đầu ra
String contains substring from list.
1Sử 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 ta đ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