算法知道是否插入,替换或删除角色(类似于Levenshtein)
我想制作一个函数,以跟踪使一个字符串与另一个字符串相同的转换
示例:
a = batyu
B =美女
diff(a,b)必须返回:
[[1,“插入”,“ e”],[5,“ delete”],[3,“插入”,“ u”]] \
i使用levenshtein.editops,但我想编码执行此操作的函数
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Wikipedia文章 levenshtein distres 可为您提供其使用的功能。现在轮到您在Python中实施它了。
如果您的代码不执行您期望的事情,请随时发布另一个问题,详细说明您的尝试,预期和为什么不起作用。
If you can read C you can also check out the implementation of editops 。
The wikipedia article for levenshtein distance gives you the function it uses. Now it's your turn to implement it in python.
If you have code that does not do what you expect it to, feel free to post another question detailing what you tried, what you expected and why it didn't work.
If you can read C you can also check out the implementation of editops.
您可以使用文档中的示例中的输出:
因此,请获取您的SequenceMatcher对象,然后迭代Opcodes并在opcodes上迭代并存储您想要的。我遇到了此搜索,以找到指向编辑文档的快速链接。出于我的目的,我以此为衡量了字符串的距离:
You can use the output from the example in the documentation https://docs.python.org/3/library/difflib.html#difflib.SequenceMatcher.get_opcodes :
so get your sequencematcher object and then iterate over the opcodes and store however you want. I came across this searching for a quick link to the editops documentation. For my purpose I used this as a measure of how close the strings were: