backtrack
backtrack

Reputation: 8154

How to get the matched string

I am using any() in python

from inActivePhrase import phrase

detailslist=[]

for detail in detailslist:
    inactive = any(term in detail for term in phrase)

Where the phrase will have the list of strings like below

phrase = ["not active","Closed",etc..]

The function is working fine. But i want to get the phrase which is present in the detail.

Example :

  detail = "this is not active"
  inactive = any(term in detail for term in phrase)
   if inactive:
        print('matched phrase' + term) //how can i do this

in which the "not active" is the matched phrase. So i want to print it.

How can i do that. Can anyone help me ?

Thanks,

Upvotes: 1

Views: 56

Answers (2)

Xavier Combelle
Xavier Combelle

Reputation: 11235

you could have several term matching detail

detail = "this is not active"
inactive = [term for term in phrase if term in detail]
if inactive:
    print('matched phrases' + inactive) 

Upvotes: 4

eumiro
eumiro

Reputation: 213075

Use next, which will iterate and stop as soon as it finds the first match. If it finds nothing, it returns the default value (None in this case):

detail = "this is not active"
inactive = next((term for term in phrase if term in detail), None)
if inactive:
    print('matched phrase' + inactive)

Upvotes: 2

Related Questions