Find subsequences of strings in strings

I want to create a function that checks a string for other lines inside them.
However, the substrings to be checked can be interrupted in the main line by other letters.

For example:

a = 'abcde' b = 'ace' c = 'acb' 

The returned function should return as b in a , but not c .

I tried set(a) . intersection (set (b)), and my problem is that it returns c as being in a .

+6
python
source share
3 answers

You can turn your expected sequence into a regular expression:

 import re def sequence_in(s1, s2): """Does `s1` appear in sequence in `s2`?""" pat = ".*".join(s1) if re.search(pat, s2): return True return False # or, more compactly: def sequence_in(s1, s2): """Does `s1` appear in sequence in `s2`?""" return bool(re.search(".*".join(s1), s2)) a = 'abcde' b = 'ace' c = 'acb' assert sequence_in(b, a) assert not sequence_in(c, a) 

"ace" becomes the regular expression "a. * c. * e", which finds these three characters in a sequence, with possible intermediate characters.

+11
source share

how about something like that ...

 def issubstr(substr, mystr, start_index=0): try: for letter in substr: start_index = mystr.index(letter, start_index) + 1 return True except: return False 

or...

 def issubstr(substr, mystr, start_index=0): for letter in substr: start_index = mystr.find(letter, start_index) + 1 if start_index == 0: return False return True 
+5
source share
 def issubstr(s1, s2): return "".join(x for x in s2 if x in s1) == s1 >>> issubstr('ace', 'abcde') True >>> issubstr('acb', 'abcde') False 
+3
source share

All Articles