python 在list中查找指定字符串的子串_Python:从列表中查找与另一个字符串最接近的字符串...

Peter Norvig提供了一篇很棒的文章,其中包含完整的源代码(21行),内容涉及拼写纠正。

[http://norvig.com/spell-correct.html]

这个想法是要对您的单词进行所有可能的编辑,

hello - helo - deletes

hello - helol - transpose

hello - hallo - replaces

hello - heallo - inserts

def edits1(word):

splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]

deletes = [a + b[1:] for a, b in splits if b]

transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]

replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]

inserts = [a + c + b for a, b in splits for c in alphabet]

return set(deletes + transposes + replaces + inserts)

现在,在列表中查找所有这些编辑。

彼得的文章读得很好,值得一读。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值