Python - find occurrences of list of strings within string

前端 未结 4 1537
不思量自难忘°
不思量自难忘° 2021-01-22 13:29

I have a large string and a list of search strings and want to build a boolean list indicating whether or not each of the search strings exists in the large string. What is the

4条回答
  •  后悔当初
    2021-01-22 13:40

    I can't say if this is the fastest, (this is still O(n*m)), but this is the way I would do it:

    def check_strings(search_list, input_string):
        return [s in input_string for s in search_list]
    

    The following program might be faster, or not. It uses a regular expression to make one pass through the input string. Note that you may you may want to use re.escape(i) in the re.findall() expression, or not, depending upon your needs.

    def check_strings_re(search_string, input_string):
        import re
        return [any(l)
                for l in
                zip(*re.findall('|'.join('('+i+')' for i in search_string),
                                input_string))]
    

    Here is a complete test program:

    def check_strings(search_list, input_string):
        return [s in input_string for s in search_list]
    
    
    def check_strings_re(search_string, input_string):
        import re
        return [any(l)
                for l in
                zip(*re.findall('|'.join('('+i+')' for i in search_string),
                                input_string))]
    
    
    search_strings = ["hello", "world", "goodbye"]
    test_string = "hello world"
    assert check_strings(search_strings, test_string) == [True, True, False]
    assert check_strings_re(search_strings, test_string) == [True, True, False]
    

提交回复
热议问题