Python - find occurrences of list of strings within string

前端 未结 4 1579
不思量自难忘°
不思量自难忘° 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 14:00

    My version using regular expressions:

    def check_strings(search_strings, input_string):
        regexp = re.compile('|'.join([re.escape(x) for x in search_strings]))
        found = set(regexp.findall(input_string))
        return [x in found for x in search_strings]
    

    On the test data provided by original poster it is by an order of magnitude slower than Rob's pretty solution, but I'm going to do some benchmarking on a bigger sample.

提交回复
热议问题