通过 difflib 模块比较 python 中的列表
我正在尝试 difflib 库。我有两个列表:L_1 和 L_2 包含字符串。我想知道这些序列是否相似(顺序并不重要)。
L_1 = ["Bob", "Mary", "Hans"]
L_2 = ["Bob", "Marie", "Háns"]
应该没问题。但
L_1 = ["Nirdosch", "Mary", "Rolf"]
L_2 = ["Bob", "Marie", "Háns"]
应该不行。
我提出了迭代第一个列表 L_1 并通过该方法与
difflib.get_close_matches()
第二个列表 L_2 匹配 L_1 的每个元素的想法。如果存在比值更大的匹配项,假设为 0.7,则将其从 L_2 中删除并继续。但我怀疑这是否是一个好的计划。还有更好的吗?
I am trying out the difflib library. I have two lists: L_1 and L_2 containing strings. I want to know, if those sequences are similar (order is not important).
L_1 = ["Bob", "Mary", "Hans"]
L_2 = ["Bob", "Marie", "Háns"]
should be ok. But
L_1 = ["Nirdosch", "Mary", "Rolf"]
L_2 = ["Bob", "Marie", "Háns"]
should not be ok.
I came up with the idea of iterating over the first list L_1 and to match every element of L_1 by the method
difflib.get_close_matches()
against the second list L_2. If there was a match with a ratio bigger then let's say 0.7 remove it from L_2 and continue. But I doubt it is a good plan. Is there a better one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会做类似的事情:
I would do something like: