这是计算Levenshtein距离的一般算法的教科书示例(我从Magnus Hetland的网站中提取):
def levenshtein(a,b):
"Calculates the Levenshtein distance between a and b."
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a,b = b,a
n,m = m,n
current = range(n+1)
for i in range(1,m+1):
previous, current = current, [i]+[0]*n
for j in range(1,n+1):
add, delete = previous[j]+1, current[j-1]+1
change = previous[j-1]
if a[j-1] != b[i-1]:
change = change + 1
current[j] = min(add, delete, change)
return current[n]
Run Code Online (Sandbox Code Playgroud)
然而,我想知道是否有更高效(可能更优雅)的纯Python实现使用difflib的SequenceManager.在玩完之后,这就是我想出的: …
我一直在尝试使用SequenceMatcher创建嵌套或递归效果.
最终目标是比较两个序列,两个序列都可能包含不同类型的实例.
例如,序列可以是:
l1 = [1, "Foo", "Bar", 3]
l2 = [1, "Fo", "Bak", 2]
Run Code Online (Sandbox Code Playgroud)
通常,SequenceMatcher仅将[1]识别为l1和l2的公共子序列.
我想SequnceMatcher被两次字符串实例应用,以便"Foo"和"Fo"作为将被视为相等,以及"Bar"和"Bak",与最长公共子序列将是长度为3的[1, Foo/Fo, Bar/Bak].也就是说,在比较字符串成员时,我希望SequenceMatcher 更宽容.
我尝试做的是为内置的str类编写一个包装器:
from difflib import SequenceMatcher
class myString:
def __init__(self, string):
self.string = string
def __hash__(self):
return hash(self.string)
def __eq__(self, other):
return SequenceMatcher(a=self.string, b=self.string).ratio() > 0.5
Run Code Online (Sandbox Code Playgroud)
编辑:也许更优雅的方式是:
class myString(str):
def __eq__(self, other):
return SequenceMatcher(a=self, b=other).ratio() > 0.5
Run Code Online (Sandbox Code Playgroud)
通过这样做,可以实现以下目标:
>>> Foo = myString("Foo")
>>> Fo = myString("Fo")
>>> …Run Code Online (Sandbox Code Playgroud) 我想实现像github 的 commit diff view这样的 diff 输出。我尝试了这个:
import difflib
first = """
def
baz
"""
second = """
deff
ba
bar
foo
"""
diff = ''
for text in difflib.unified_diff(first, second):
for prefix in ('---', '+++', '@@'):
if text.startswith(prefix):
break
else:
diff += text
Run Code Online (Sandbox Code Playgroud)
输出是:
d e f+f
b a-z
+b+a+r+
+f+o+o+
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现,
1 def+f
2 ba-z
+
3 bar
4 foo
# -
# 5 line
# 6 line
Run Code Online (Sandbox Code Playgroud)
像这样的输出。谢谢。
以下是两个数组:
import difflib
import scipy
import numpy
a1=numpy.array(['198.129.254.73','134.55.221.58','134.55.219.121','134.55.41.41','198.124.252.101'], dtype='|S15')
b1=numpy.array(['198.124.252.102','134.55.41.41','134.55.219.121','134.55.219.137','134.55.220.45', '198.124.252.130'],dtype='|S15')
difflib.get_close_matches(a1[-1],b1,2)
Run Code Online (Sandbox Code Playgroud)
输出:
['198.124.252.130', '198.124.252.102']
Run Code Online (Sandbox Code Playgroud)
不应该'198.124.252.102'是最接近的匹配'198.124.252.101'?
我查看了他们已经指定了一些浮动类型权重的文档,但没有关于算法使用的信息.
我需要找出最后两个八位位组之间的绝对差值是否为1(假设前三个八位位组相同).
所以我首先找到最接近的字符串然后检查上述条件的最接近的字符串.
有没有其他功能或方法来实现这一目标?另外get_close_matches()表现如何?
ipaddr 似乎对ips没有这样的操纵.
在 python difflib 库中,SequenceMatcher 类的行为是否出乎意料,还是我误读了假定的行为?
为什么 isjunk 论点在这种情况下似乎没有任何区别?
difflib.SequenceMatcher(None, "AA", "A A").ratio() return 0.8
difflib.SequenceMatcher(lambda x: x in ' ', "AA", "A A").ratio() returns 0.8
Run Code Online (Sandbox Code Playgroud)
我的理解是,如果省略空格,则比率应为1。
我正在尝试检查线路之间的差异。这是我的代码:
from difflib import unified_diff
s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']
for line in unified_diff(s1, s2):
print line
Run Code Online (Sandbox Code Playgroud)
它打印:
---
+++
@@ -4,3 +4,9 @@
d
e
f
+g
+i
+k
+l
+m
+n
Run Code Online (Sandbox Code Playgroud)
“a”、“b”和“c”发生了什么?谢谢!
从两个多线字符串中获得差异的最佳方法是什么?
a = 'testing this is working \n testing this is working 1 \n'
b = 'testing this is working \n testing this is working 1 \n testing this is working 2'
diff = difflib.ndiff(a,b)
print ''.join(diff)
Run Code Online (Sandbox Code Playgroud)
这会产生:
t e s t i n g t h i s i s w o r k i n g
t e s t i n g t h i s i s w o r k i n g 1
+ …Run Code Online (Sandbox Code Playgroud) 我有很多字符串,我想匹配相似性(每个字符串平均30个字符).我发现difflib's SequenceMatcher这个任务很棒,因为它很简单,结果很好.但如果我比较hellboy并hell-boy喜欢这个
>>> sm=SequenceMatcher(lambda x:x=='-','hellboy','hell-boy')
>>> sm.ratio()
0: 0.93333333333333335
Run Code Online (Sandbox Code Playgroud)
我希望这样的话能给出100%的匹配,即ratio of 1.0.我知道上面函数中指定的垃圾字符不用于比较,而是查找最长的连续匹配子序列.为了比较的目的,有什么办法可以SequenceMatcher忽略一些"垃圾"字符吗?
假设我想将文件 a 和文件 b 与该difflib.diff_bytes函数进行比较,我该怎么做?
谢谢
difflib ×10
python ×10
algorithm ×2
string ×2
file ×1
ip ×1
performance ×1
python-2.7 ×1
python-3.x ×1
sdiff ×1
tkinter ×1
unified-diff ×1
unix ×1