Big geez
Big geez

Reputation: 39

How to check if a certain part of a string is in a set of strings in Python?

If I have a list of strings

set1 = ['\\land','\\lor','\\implies']

I want to scan a list of strings and check whether any of the strings contain the set elements within them.

The string '\land' would return true for being in set1

However, how would I check whether '(\lor' is in set1?

Upvotes: 0

Views: 230

Answers (1)

Sankar
Sankar

Reputation: 586

See if this works for you:

import re

set1 = ['\\land','\\lor','\\implies']
strings = ['\land', '(\lor']

r = re.compile('|'.join([re.escape(w) for w in set1]), flags=re.I)

for i in strings:
    print(r.findall(i))

The output of this is

['\\land']
['\\lor']

**** Modification - if there is one string**

import re

set1 = ['\\land','\\lor','\\implies']
strings = '(\lor'

r = re.compile('|'.join([re.escape(w) for w in set1]), flags=re.I)

print(r.findall(strings))

** if you just want the special character "(" to be out from "(\lor" we could do that by this :

>>> a = '(\lor'
>>> a.split('(')[1]
'\\lor'

Upvotes: 1

Related Questions