Dani Valverde
Dani Valverde

Reputation: 409

How to match strings even if only they match partially?

I have two lists which I'd like to compare and if there is any match (even if partial) then carry out some action. I've set up this test code:

keywords = ['social media','social business','social networking','social marketing','online marketing','social selling',
    'social customer experience management','social cxm','social cem','social crm','google analytics','seo','sem',
    'digital marketing','social media manager','community manager']

metakeywords = ['top 10', 'social media blog', 'social media blog nomination']

if any(key in metakeywords for key in keywords):
    print 'Ok'

As you can see, there is a partial match between the 1st item of keywords and the 2nd and 3rd item of metakeywords, so it should print Ok. How can I do this? Thanks!

Dani

Upvotes: 3

Views: 233

Answers (1)

khelwood
khelwood

Reputation: 59103

If you want to find out if any item in keywords is contained in any item in metakeywords, you can do this:

if any(key in metakey for key in keywords for metakey in metakeywords):
    print 'ok'

Upvotes: 7

Related Questions