【问题标题】:Python: Passing SequenceMatcher in difflib an "autojunk=False" flag yields errorPython:在 difflib 中传递 SequenceMatcher 一个“autojunk=False”标志会产生错误
【发布时间】:2014-01-02 03:18:41
【问题描述】:

trying使用Python的difflib包中的SequenceMatcher方法来识别字符串相似度。不过,我遇到了这种方法的奇怪行为,我相信我的问题可能与包的“垃圾”过滤器有关,这个问题在here 中有详细描述。我只想说我认为我可以通过以difflib documentation 描述的方式将自动垃圾标志传递给我的 SequenceMatcher 来解决我的问题:

import difflib

def matches(s1, s2):
    s = difflib.SequenceMatcher(None, s1, s2, autojunk=False)
    match = [s1[i:i+n] for i, j, n in s.get_matching_blocks() if n > 0]
    return match

print matches("they all are white a sheet of spotless paper when they first are born but they are to be scrawled upon and blotted by every goose quill", "you are all white a sheet of lovely spotless paper when you first are born but you are to be scrawled and blotted by every gooses quill")

但这会产生以下错误消息:

Traceback (most recent call last):
  File "test3.py", line 8, in <module>
    print matches("they all are white a sheet of spotless paper when they first are born but they are to be scrawled upon and blotted by every goose quill", "you are all white a sheet of lovely spotless paper when you first are born but you are to be scrawled and blotted by every gooses quill")
  File "test3.py", line 4, in matches
    s = difflib.SequenceMatcher(None, s1, s2, autojunk=False)
TypeError: __init__() got an unexpected keyword argument 'autojunk'

有谁知道我如何将 autojunk=False 标志传递给 SequenceMatcher?如果其他人可以提供任何建议,我将不胜感激。

【问题讨论】:

  • 你用的是哪个版本的python?

标签: python design-patterns flags difflib sequencematcher


【解决方案1】:

根据SequenceMatcher documentation

可选参数 autojunk 可用于禁用自动垃圾启发式。

2.7.1 版本中的新功能:autojunk 参数。

升级到 Python 2.7.1+ 以使用 autojunk 参数。

【讨论】:

  • 更好,升级到 2.7.6 - 真的没有充分的理由“升级”到过时的版本 ;-)
猜你喜欢
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 2023-03-24
  • 2019-07-18
  • 2018-07-10
  • 1970-01-01
相关资源
最近更新 更多