Python 在字符串中查找字符串的子序列
我想创建一个函数,用于检查字符串中是否存在其他字符串。Python 在字符串中查找字符串的子序列,python,Python,我想创建一个函数,用于检查字符串中是否存在其他字符串。 但是,正在检查的子字符串可能会在主字符串中被其他字母打断 例如: a = 'abcde' b = 'ace' c = 'acb' 相关函数应返回为b位于a,而不是c 我试过set(a)。交叉点(集合(b))已经存在,我的问题是它返回c作为a中的值 您可以将预期序列转换为正则表达式: import re def sequence_in(s1, s2): """Does `s1` appear in sequence in `s2`
但是,正在检查的子字符串可能会在主字符串中被其他字母打断 例如:
a = 'abcde'
b = 'ace'
c = 'acb'
相关函数应返回为b
位于a
,而不是c
我试过
set(a)
。交叉点(集合(b))已经存在,我的问题是它返回c
作为a
中的值 您可以将预期序列转换为正则表达式:
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”被转换成正则表达式“a.*c.*e”,它按顺序查找这三个字符,可能包含中间字符。类似这样的内容如何
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
或者
我希望这比基于正则表达式的答案运行得更快。你有时间安排吗?没有时间安排,只是作为一种选择。请解释空白注释。问题是找到子序列,而不是子字符串。这些类型的字符串被称为长字符串。这个问题是解决方案的一个特例,解决这个问题的效率也更高。
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
def issubstr(s1, s2):
return "".join(x for x in s2 if x in s1) == s1
>>> issubstr('ace', 'abcde')
True
>>> issubstr('acb', 'abcde')
False