How get all matches using str.contains in python regex?

前端 未结 2 1328
闹比i
闹比i 2021-01-27 03:37

I have a data frame, in which I need to find all the possible matches rows which match with terms. My code is

texts = [\'foo abc\', \'foobar xyz\',         


        
2条回答
  •  孤独总比滥情好
    2021-01-27 03:54

    Instead of using the regex pattern for checking the presence of terms,

    #create pattern
    p = re.compile(pat)
    
    #search for pattern in the column
    results = [p.findall(text) for text in df.Match_text.tolist()]
    

    Try using a simple lookup of terms in the text like this.

    #search for each term in the column
    results = [[term for term in terms if term in text] for text in df.Match_text.tolist()]
    

    Output for the above looks like this,

        Match_text  results
    0   foo abc [foo]
    3   baz 45  [baz]
    6   foo baz [foo, baz, foo baz]
    

    NOTE : There is a time complexity associated to this method.

提交回复
热议问题