Hướng dẫn how do i check if a string subsets in python? - làm cách nào để kiểm tra xem một tập hợp con chuỗi trong python?
Cải thiện bài viết Show
Lưu bài viết Đôi khi, trong khi làm việc với các chuỗi, chúng ta có thể có một vấn đề trong đó chúng ta cần kiểm tra xem một chuỗi là một phần sau của một chuỗi khác. Điều này có thể có ứng dụng có thể trong nhiều lĩnh vực bao gồm khoa học dữ liệu và lập trình hàng ngày. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện. & NBSP; Phương pháp số 1: Sử dụng tất cả () Đây là một trong những điều mà chúng ta có thể giải quyết vấn đề này. Trong đó, chúng tôi sử dụng tất cả () để kiểm tra xem tất cả các ký tự của một chuỗi có mặt trong một chuỗi khác. & NBSP; This is one of the by which we can solve this problem. In this, we employ all() to check if all the characters of one string are present in another. Python3
The original string is : geeksforgeeks Does string contains all the characters of other list? : True0 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True5 The original string is : geeksforgeeks Does string contains all the characters of other list? : True6 The original string is : geeksforgeeks Does string contains all the characters of other list? : True7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True8 = Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes0 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes1 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Yes! it is present in the string0 The original string is : geeksforgeeks Does string contains all the characters of other list? : True6 Yes! it is present in the string2 Yes! it is present in the string3 Đầu ra: & nbsp; The original string is : geeksforgeeks Does string contains all the characters of other list? : True & nbsp; Phương pháp số 2: Sử dụng assubset () sử dụng hàm sẵn là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chỉ sử dụng chức năng và nó trả về kết quả sau khi xử lý nội bộ. & NBSP;Method #2 : Using issubset() Using an inbuilt function is one of the ways in which this task can be performed. In this, we just employ the function and it returns the result after internal processing. Python3
The original string is : geeksforgeeks Does string contains all the characters of other list? : True0 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True5 The original string is : geeksforgeeks Does string contains all the characters of other list? : True6 The original string is : geeksforgeeks Does string contains all the characters of other list? : True7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True8 = Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes0 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes1 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Yes! it is present in the string0 The original string is : geeksforgeeks Does string contains all the characters of other list? : True6 Yes! it is present in the string2 Yes! it is present in the string3 Đầu ra: & nbsp; The original string is : geeksforgeeks Does string contains all the characters of other list? : True & nbsp; Phương pháp số 2: Sử dụng assubset () sử dụng hàm sẵn là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chỉ sử dụng chức năng và nó trả về kết quả sau khi xử lý nội bộ. & NBSP;
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:O(n) The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes31 "geeksforgeeks" 0 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True49 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes2 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes Phương pháp: Sử dụng chức năng LambdaCácin” operator which is used as a comparison operator. Here we will cover different approaches like:
Có, kiểm tra một chuỗi con là một trong những nhiệm vụ được sử dụng nhiều nhất trong Python. Python sử dụng nhiều phương thức để kiểm tra một chuỗi có chứa chuỗi con như, find (), index (), Count (), v.v. . Ở đây chúng tôi sẽ bao gồm các cách tiếp cận khác nhau như:Python3Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.5 = Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True99 = test_str1 9NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'5 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'8 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 test_str1 2NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 Đầu ra Yes! it is present in the string Phương pháp 7: Kiểm tra chuỗi con & nbsp; sử dụng biểu thức thông thường & nbsp;Regex có thể được sử dụng để kiểm tra xem một chuỗi có chứa mẫu tìm kiếm được chỉ định không. Python có một gói tích hợp có tên Re, có thể được sử dụng để làm việc với các biểu thức thông thường. & NBSP; Python3The original string is : geeksforgeeks Does string contains all the characters of other list? : True94 The original string is : geeksforgeeks Does string contains all the characters of other list? : True95 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.5 = Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True99 = test_str1 9Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes03 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 "geeksforgeeks" 0NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'8 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 "geeksforgeeks" 7NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 2The original string is : geeksforgeeks Does string contains all the characters of other list? : True3The original string is : geeksforgeeks Does string contains all the characters of other list? : True4Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes07 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09The original string is : geeksforgeeks Does string contains all the characters of other list? : True4NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes18 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Python3
The original string is : geeksforgeeks Does string contains all the characters of other list? : True00 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True03 = = The original string is : geeksforgeeks Does string contains all the characters of other list? : True06 The original string is : geeksforgeeks Does string contains all the characters of other list? : True07 The original string is : geeksforgeeks Does string contains all the characters of other list? : True08 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True12 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True20 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6
The original string is : geeksforgeeks Does string contains all the characters of other list? : True25 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True27 The original string is : geeksforgeeks Does string contains all the characters of other list? : True28 Phương pháp 4: Kiểm tra Subring & NBSP; sử dụng phương thức đếm () Phương thứcBạn cũng có thể đếm số lần xuất hiện của một chuỗi con cụ thể trong một chuỗi, sau đó bạn có thể sử dụng phương thức Python Count (). Nếu không tìm thấy chất nền thì không có, thì có, sẽ không in nếu không sẽ không được in. Python3
The original string is : geeksforgeeks Does string contains all the characters of other list? : True30 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True33 The original string is : geeksforgeeks Does string contains all the characters of other list? : True34 The original string is : geeksforgeeks Does string contains all the characters of other list? : True08 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True20 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True12 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 The original string is : geeksforgeeks Does string contains all the characters of other list? : True49 = Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True52 = test_str1 9The original string is : geeksforgeeks Does string contains all the characters of other list? : True55 Phương pháp 5: Kiểm tra chuỗi con bằng phương thức Index ()Phương thức .index () Trả về chỉ số bắt đầu của chuỗi con được truyền dưới dạng tham số. Ở đây, Sub Substring có mặt tại INDEX 16..index() method returns the starting index of the substring passed as a parameter. Here “substring” is present at index 16. Python3The original string is : geeksforgeeks Does string contains all the characters of other list? : True56 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True58 The original string is : geeksforgeeks Does string contains all the characters of other list? : True59 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True34 The original string is : geeksforgeeks Does string contains all the characters of other list? : True62 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True64 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True66 The original string is : geeksforgeeks Does string contains all the characters of other list? : True67 The original string is : geeksforgeeks Does string contains all the characters of other list? : True68 Output: 16 Phương pháp 6: Kiểm tra chuỗi con & nbsp; sử dụng lớp __contains __, & nbsp; lớp ma thuật.Chuỗi Python __Contains __ (). Phương thức này được sử dụng để kiểm tra xem chuỗi có mặt trong chuỗi khác hay không. & NBSP; Python3The original string is : geeksforgeeks Does string contains all the characters of other list? : True69 = The original string is : geeksforgeeks Does string contains all the characters of other list? : True71 The original string is : geeksforgeeks Does string contains all the characters of other list? : True72 The original string is : geeksforgeeks Does string contains all the characters of other list? : True73 The original string is : geeksforgeeks Does string contains all the characters of other list? : True74 The original string is : geeksforgeeks Does string contains all the characters of other list? : True73 The original string is : geeksforgeeks Does string contains all the characters of other list? : True76__ Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True81 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True83 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 The original string is : geeksforgeeks Does string contains all the characters of other list? : True86 The original string is : geeksforgeeks Does string contains all the characters of other list? : True87 The original string is : geeksforgeeks Does string contains all the characters of other list? : True08 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True91 The original string is : geeksforgeeks Does string contains all the characters of other list? : True92 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'6 Đầu ra Yes! Geeks-13 is containing. Yes! Geeks-78 is containing. Phương pháp 7: Kiểm tra chuỗi con & nbsp; sử dụng biểu thức thông thường & nbsp;Regex có thể được sử dụng để kiểm tra xem một chuỗi có chứa mẫu tìm kiếm được chỉ định không. Python có một gói tích hợp có tên Re, có thể được sử dụng để làm việc với các biểu thức thông thường. & NBSP;re, which can be used to work with Regular Expressions. Python3The original string is : geeksforgeeks Does string contains all the characters of other list? : True94 The original string is : geeksforgeeks Does string contains all the characters of other list? : True95 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.5 = Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True99 = test_str1 9Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.8 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes03 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes07 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes12 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'8 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes18 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 The original string is : geeksforgeeks Does string contains all the characters of other list? : True09 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes12 Đầu ra NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' Phương pháp 7: Kiểm tra chuỗi con & nbsp; sử dụng biểu thức thông thường & nbsp;Python3Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes24 = test_str1 6Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes27 = test_str1 9Regex có thể được sử dụng để kiểm tra xem một chuỗi có chứa mẫu tìm kiếm được chỉ định không. Python có một gói tích hợp có tên Re, có thể được sử dụng để làm việc với các biểu thức thông thường. & NBSP; The original string is : geeksforgeeks Does string contains all the characters of other list? : True94 The original string is : geeksforgeeks Does string contains all the characters of other list? : True95Python3Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes24 = test_str1 6Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes27 = test_str1 9Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.5 = Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.7 The original string is : geeksforgeeks Does string contains all the characters of other list? : True99 = test_str1 9
Python3NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes07 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes24 = test_str1 6NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'2 The original string is : geeksforgeeks Does string contains all the characters of other list? : True3 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes18 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes08 Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes09 The original string is : geeksforgeeks Does string contains all the characters of other list? : True4 Phương pháp: Sử dụng danh sách hiểu & nbsp; |