例如,我的词是“大脑”,我们需要创建一个函数来检查它是不是替代和插入。 例如,替代品可以是:训练粒痘。。。插入:barain bryain
#I am not sure I did the right way or missing something. And any other way that faster?
#string0 is pattern
def subsitutions(string0, string1):
sublist = []
for i in range(len(string0)):
sublist.append(string0[:i] + string0[i + 1:])
for i in range(len(string1)):
if string1[:i] + string1[i + 1:] in sublist:
print("This word is subsitutions")
break
def insertions(string0, string1):
if len(string0)+1 == len(string1):
for i in range(len(string1)):
if string1[:i] + string1[i + 1:] == string0:
print("This word is insertions")
break
看看这是否有用
set.difference
很容易为您做到这一点:下面是这张支票的一个肮脏的例子:
您需要检查每个字符串的长度,因为仅仅使用
set
并不能适用于所有字符串相关问题 更多 >
编程相关推荐