Danh sách chuỗi python chứa chuỗi con
Để 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 Show
Trong loại tình huống này, chúng ta có
ví dụ 1. Kiểm tra xem Chuỗi có chứa Chuỗi con từ Danh sách khôngTrong 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 Chạyđầu ra
Vì mục từ danh sách 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ử 24, điều kiện trong vòng lặp if trở thành TrueBản tóm tắtTrong 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. 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ụ: 27 đánh giá thành 28 nếu 29 là thành viên của 30, nếu không, nó đánh giá thành 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 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 33Biến 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ề 28 nếu bất kỳ phần tử nào trong iterable là đúngNếu chức năng 270 tìm thấy một mục phù hợp, nó sẽ đoản mạch trả về 28Ngoài ra, bạn có thể sử dụng vòng lặp 272 đơn giảnVòng lặp 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 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 41 42 43 44
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 10 11 12
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 16 11 18 19
42 43 12 13 14
15 43 17 18 19 10 11 12 18 14 10 16 17 18 19____143 43 412 413 15____143 416 413 418 18 15 43 43 416 424 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 428 14 430 424 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 435 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ôngVấ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 437 438 439
41 42 43 44
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 10 11 12
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 16 11 18 19
439 438The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element07 43 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element09 18 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element11 10 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0 18The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element15 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 11 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 1Sử dụng any() để kiểm tra xem chuỗi có chứa phần tử từ danh sách khôngSử 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 438 439
41 42 43 44
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 10 11 12
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 16 11 18 19
439 438The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element07 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 10 42 18 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element11 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 11 18The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element72 đầu ra 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 |