0

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?

5
  • 1
    what have you tried? Technically set1 is a list, not set. And \land is not in set1, unless you look for some partial/fuzzy match. Nor list elements are in \land string. In this case you need to clarify your question/provide more info. Commented Mar 7, 2020 at 17:44
  • I'm confused... (\lor isn't in set1 but one of the items in set1 is in (\lor. Is that what you mean? Then any(s in "(\\lor" for s in set1) would do. Commented Mar 7, 2020 at 17:48
  • Yeah you're right, it's a list. However '\land' is in set1 because, '\\land' is how Python will store '\land'. Commented Mar 7, 2020 at 17:50
  • @tdelaney , basically I want strings like '(\lor', 'xyz\lor' and '))\lor' to return true for being in set1 because the strings contain '/lor', which is an element of set1. Commented Mar 7, 2020 at 17:53
  • Yeah, my bad, I overlooked the escape sequence Commented Mar 7, 2020 at 18:00

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, how would I alter this if I was to just check one string instead of a list of strings?
Do you know how I could split the string in to two parts as well? If I had '(\lor' how could I get '(' and '\\lor'
@Biggeez, I have updated the answer, see if this is what you needed
More specifically, if I had 'Predicate(big,small)' string, how could I split that up in to a list of size six; ['Predicate','(','big','small',')']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.