拼写检查我想在拼写中添加两个缺失的字母。 请建议我如何在python中做到这一点。 我有添加一个字母的代码。
def splits(word):
result = []
for i in range(len(word) + 1):
result.append((word[:i], word[i:]))
print result
return result
def inserts(word):
result = []
for a, b in splits(word):
for c in alphabet:
result.append(a + c + b)
return result
例如,我有单词He
我想要结果Haeb
,aHeb
,abHe
等
答案 0 :(得分:2)
试试这个:
def insertEach(chars, original):
if not chars:
yield original
else:
for doted in insertEach(chars[1:], original):
for i in range(len(doted) + 1):
yield doted[:i] + chars[0] + doted[i:]
[ x for x in insertEach('ab', 'HI') ]
结果:
['abHI', 'baHI', 'bHaI', 'bHIa', 'aHbI', 'HabI', 'HbaI', 'HbIa', 'aHIb', 'HaIb', 'HIab', 'HIba']