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

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 True

Bả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ỉ định

Ví 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.
31

Danh 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án

Biể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.
33

Biế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ỉ định

Hà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à đúng

Nế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.
28

Ngoài ra, bạn có thể sử dụng vòng lặp

String contains substring from list.
272 đơn giản

Vò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ông

Khi 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.
41

String contains substring from list.
42
String contains substring from list.
43
String contains substring from list.
44

 

String contains substring from list.
45

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0______143
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2____23
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
String contains substring from list.
10
String contains substring from list.
11
String contains substring from list.
12

 

String contains substring from list.
13

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
String contains substring from list.
16
String contains substring from list.
11
String contains substring from list.
18
String contains substring from list.
19

 

String contains substring from list.
42
String contains substring from list.
43
String contains substring from list.
12
String contains substring from list.
13
String contains substring from list.
14

 

String contains substring from list.
15
String contains substring from list.
43
String contains substring from list.
17

String contains substring from list.
18
String contains substring from list.
19
String contains substring from list.
10
String contains substring from list.
11

String contains substring from list.
12
String contains substring from list.
18
String contains substring from list.
14
String contains substring from list.
10
String contains substring from list.
16

String contains substring from list.
17
String contains substring from list.
18
String contains substring from list.
19____143
String contains substring from list.
43
String contains substring from list.
412

String contains substring from list.
413
String contains substring from list.
15____143
String contains substring from list.
416

String contains substring from list.
413
String contains substring from list.
418

String contains substring from list.
18
String contains substring from list.
15
String contains substring from list.
43
String contains substring from list.
43
String contains substring from list.
416
String contains substring from list.
424

String contains substring from list.
12
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
String contains substring from list.
428
String contains substring from list.
14

String contains substring from list.
430
String contains substring from list.
424

String contains substring from list.
12
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
String contains substring from list.
435
String 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.
437

String contains substring from list.
438

String contains substring from list.
439

 

String contains substring from list.
41

String contains substring from list.
42
String contains substring from list.
43
String contains substring from list.
44

 

String contains substring from list.
45

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0______143
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2____23
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
String contains substring from list.
10
String contains substring from list.
11
String contains substring from list.
12

 

String contains substring from list.
13

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
String contains substring from list.
16
String contains substring from list.
11
String contains substring from list.
18
String contains substring from list.
19

 

String contains substring from list.
439

String contains substring from list.
438

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
07
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 element
09
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 element
11
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 element
0
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 element
15
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 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
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
21
String contains substring from list.
11
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 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

String contains substring from list.
1

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




String contains substring from list.
438

String contains substring from list.
439

 

String contains substring from list.
41

String contains substring from list.
42
String contains substring from list.
43
String contains substring from list.
44

 

String contains substring from list.
45

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
0______143
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
2____23
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
String contains substring from list.
10
String contains substring from list.
11
String contains substring from list.
12

 

String contains substring from list.
13

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
String contains substring from list.
16
String contains substring from list.
11
String contains substring from list.
18
String contains substring from list.
19

 

String contains substring from list.
439

String contains substring from list.
438

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
07
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 element
58
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
15
String contains substring from list.
10
String contains substring from list.
42
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 element
11
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 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
The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
String contains the list element
21
String contains substring from list.
11
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 element
72

đầu ra

String contains substring from list.
1

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

Chủ Đề