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 Show
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ôngHã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ạnhNế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 Python3Found 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 element0 Found it!28 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3 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____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 element8 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9____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 element8____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 element8____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ô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 . Python3Found 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 element0 Found it!28 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3 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____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 element8 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9____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 element07 Found it!28 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element09 Found it!38 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element11 Found it!30 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element0 Found it!38 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element15 Found it!30 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____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 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 Found it!3 Sử 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. Python3Found 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 element0 Found it!28 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element2 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element3 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____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 element8 The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element9____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 element07 Found it!28 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 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 element11 Found it!30 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____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 element72 đầ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 |