Hướng dẫn find substring in set python - tìm chuỗi con trong tập python

Tìm một chuỗi con trong một set []:

name = set[]
name.add['Victoria Stuart']                         ## add single element
name.update[['Carmine Wilson', 'Jazz', 'Georgio']]  ## add multiple elements
name
{'Jazz', 'Georgio', 'Carmine Wilson', 'Victoria Stuart'}

me = 'Victoria'
if str[name].find[me]:
    print['{} in {}'.format[me, name]]
# Victoria in {'Jazz', 'Georgio', 'Carmine Wilson', 'Victoria Stuart'}

Điều đó khá dễ dàng - nhưng hơi có vấn đề, nếu bạn muốn trả về chuỗi phù hợp:

for item in name:
    if item.find[me]:
            print[item]
'''
Jazz
Georgio
Carmine Wilson
'''

print[str[name].find[me]]
# 39    ## character offset for match [i.e., not a string]

Như bạn có thể thấy, vòng lặp ở trên chỉ thực thi cho đến khi điều kiện là True, chấm dứt trước khi in mục chúng tôi muốn [chuỗi khớp].

Có lẽ tốt hơn, sử dụng regex [biểu thức thông thường] dễ dàng hơn:

import re

for item in name:
    if re.match[me, item]:
            full_name = item
            print[item]
# Victoria Stuart
print[full_name]
# Victoria Stuart

for item in name:
    if re.search[me, item]:
            print[item]
# Victoria Stuart

Từ các tài liệu Python:

search [] so với match []

Python cung cấp hai hoạt động nguyên thủy khác nhau dựa trên các biểu thức thông thường: re.match[] chỉ kiểm tra một trận đấu ở đầu chuỗi, trong khi re.search[] kiểm tra đối sánh ở bất cứ đâu trong chuỗi ...

Phương pháp số 2: Sử dụng bất kỳ [] bất kỳ chức năng nào cũng có thể được sử dụng để tính toán sự hiện diện của chuỗi con thử nghiệm trong tất cả các chuỗi của danh sách và trả về true nếu nó được tìm thấy trong bất kỳ. Điều này tốt hơn hàm trên vì nó không chiếm không gian rõ ràng để tạo chuỗi được nối mới.

Làm cách nào để tìm thấy một chuỗi con trong một chuỗi?

Chạy một vòng từ đầu đến cuối và cho mọi chỉ mục trong chuỗi đã cho kiểm tra xem chuỗi con có thể được hình thành từ chỉ mục đó hay không. Điều này có thể được thực hiện bằng cách chạy một vòng lặp lồng nhau đi qua chuỗi đã cho và trong vòng lặp đó chạy vòng lặp khác kiểm tra các chuỗi phụ bắt đầu từ mọi chỉ mục.

  • Xem thảo luận
  • Cải thiện bài viết
  • Phương pháp số 2: Sử dụng bất kỳ [] bất kỳ chức năng nào cũng có thể được sử dụng để tính toán sự hiện diện của chuỗi con thử nghiệm trong tất cả các chuỗi của danh sách và trả về true nếu nó được tìm thấy trong bất kỳ. Điều này tốt hơn hàm trên vì nó không chiếm không gian rõ ràng để tạo chuỗi được nối mới.

    Làm cách nào để tìm thấy một chuỗi con trong một chuỗi?

    Chạy một vòng từ đầu đến cuối và cho mọi chỉ mục trong chuỗi đã cho kiểm tra xem chuỗi con có thể được hình thành từ chỉ mục đó hay không. Điều này có thể được thực hiện bằng cách chạy một vòng lặp lồng nhau đi qua chuỗi đã cho và trong vòng lặp đó chạy vòng lặp khác kiểm tra các chuỗi phụ bắt đầu từ mọi chỉ mục.

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết
    List comprehension is an elegant way to perform any particular task as it increases readability in a long run. This task can be performed using naive method and hence can be reduced to list comprehension as well.

    test_list

    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    1
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    2
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    4
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    8
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    9

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    5

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    8

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    5

    Output:

    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    

    & nbsp; Phương pháp #3: Sử dụng các biểu thức re.search[]333 có thể được sử dụng để thực hiện nhiều nhiệm vụ trong Python. Để thực hiện nhiệm vụ cụ thể này, các biểu thức thường xuyên có thể có ích. Nó tìm thấy tất cả các chuỗi con phù hợp bằng cách sử dụng kết quả re.search[]4 và trả về.
    Method #2 : Using

    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    6 + lambda
    This function can also perform this task of finding the strings with the help of lambda. It just filters out all the strings matching the particular substring and then adds it in a new list.

    test_list

    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    1
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    2
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    4
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    8
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    9

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    5

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    8

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    5

    Output:

    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    

    & nbsp; Phương pháp #3: Sử dụng các biểu thức re.search[]333 có thể được sử dụng để thực hiện nhiều nhiệm vụ trong Python. Để thực hiện nhiệm vụ cụ thể này, các biểu thức thường xuyên có thể có ích. Nó tìm thấy tất cả các chuỗi con phù hợp bằng cách sử dụng kết quả re.search[]4 và trả về.
    Method #3 : Using re.search[]3
    Regular expressions can be used to perform many task in python. To perform this particular task also, regular expressions can come handy. It finds all the matching substring using re.search[]4 and returns result.

    re.search[]5 re.search[]6

    test_list

    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    1
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    2
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    4
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    3
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    8
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    9

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    5

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    6
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    8

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    9
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    0
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    09
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    2
    for item in name:
        if item.find[me]:
                print[item]
    '''
    Jazz
    Georgio
    Carmine Wilson
    '''
    
    print[str[name].find[me]]
    # 39    ## character offset for match [i.e., not a string]
    
    11
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    4

    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    0
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    1
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    2
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    3
    import re
    
    for item in name:
        if re.match[me, item]:
                full_name = item
                print[item]
    # Victoria Stuart
    print[full_name]
    # Victoria Stuart
    
    for item in name:
        if re.search[me, item]:
                print[item]
    # Victoria Stuart
    
    4
    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    
    5

    Output:

    The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
    All strings with given substring are : ['GeeksforGeeks', 'Geeky']
    


    Làm thế nào để bạn tìm thấy chuỗi con của một bộ trong Python?

    Sử dụng Find []: Find [] là một phương thức khác có thể được sử dụng để kiểm tra truy vấn của chúng tôi. Tương tự như phương thức index [], find [] cũng trả về chỉ mục bắt đầu của chuỗi con. Tuy nhiên, hãy tìm [] trả về -1 trong trường hợp chuỗi con không tồn tại. -1 là chỉ số âm của ký tự ngoài cùng bên trái. find[] is another method that can be used to check our query. Similar to the index[] method, find[] also returns the starting index of the substring. However, find[] returns -1 in case the substring does not exist. -1 is the negative index of the leftmost character.

    Làm thế nào để bạn kiểm tra xem một chuỗi nằm trong một bộ python?

    Để kiểm tra xem tập hợp có chứa một phần tử trong Python hay không, hãy sử dụng từ khóa, trả về true nếu tập hợp được chỉ định chứa một phần tử và sai.Từ khóa kiểm tra nếu mục có mặt trong một chuỗi như danh sách, phạm vi, chuỗi, bộ, v.v.use the in keyword, which returns True if the specified Set contains an element and False otherwise. The in keyword checks if the item is present in a sequence like a list, range, string, set, etc.

    Làm thế nào để bạn kiểm tra xem một chuỗi con có nằm trong danh sách các chuỗi Python không?

    Phương pháp số 2: Sử dụng bất kỳ [] bất kỳ chức năng nào cũng có thể được sử dụng để tính toán sự hiện diện của chuỗi con thử nghiệm trong tất cả các chuỗi của danh sách và trả về true nếu nó được tìm thấy trong bất kỳ.Điều này tốt hơn hàm trên vì nó không chiếm không gian rõ ràng để tạo chuỗi được nối mới.Using any[] The any function can be used to compute the presence of the test substring in all the strings of the list and return True if it's found in any. This is better than the above function as it doesn't explicitly take space to create new concatenated string.

    Làm cách nào để tìm thấy một chuỗi con trong một chuỗi?

    Chạy một vòng từ đầu đến cuối và cho mọi chỉ mục trong chuỗi đã cho kiểm tra xem chuỗi con có thể được hình thành từ chỉ mục đó hay không.Điều này có thể được thực hiện bằng cách chạy một vòng lặp lồng nhau đi qua chuỗi đã cho và trong vòng lặp đó chạy vòng lặp khác kiểm tra các chuỗi phụ bắt đầu từ mọi chỉ mục.

    Bài Viết Liên Quan

    Chủ Đề