我正在尝试比较两个1000字节的字符串,并想知道区别开始的位置,也就是说,哪个字节是不同的...是否有函数可以确定它?
next
和生成器(generator)?next(idx for idx,c in enumerate(your_string1) if c != your_string2[idx])
这将给你一个不同之处开始的索引,并在它们相等时引发StopIteration
。
如果使用itertools.izip
,可能会更加优雅:
next(idx for idx,(c1,c2) in enumerate(izip(s1,s2)) if c1 != c2)
例子:
>>> from itertools import izip
>>> s1 = 'stop at the bus'
>>> s2 = 'stop at the car'
>>> next(idx for idx,(c1,c2) in enumerate(izip(s1,s2)) if c1 != c2)
12
>>> s1[12]
'b'
>>> s2[12]
'c'
我尝试测试了这里提供的答案,并且找到了另一种解决方案,通常情况下更快,虽然不太优雅。
首先让我们看看提出的解决方案有多快:
In [15]: def check_genexp(a, b):
...: return next(idx for idx, c in enumerate(a) if c != b[idx])
In [16]: %timeit check_genexp("a"*9999 + "b", "a"*9999 + "c")
1000 loops, best of 3: 1.04 ms per loop
In [17]: from difflib import SequenceMatcher
In [18]: def check_matcher(a, b):
...: return next(SequenceMatcher(a=a, b=b).get_matching_blocks())
...:
In [19]: %timeit check_matcher("a"*9999+"b", "a"*9999+"c")
100 loops, best of 3: 11.5 ms per loop
def binary_check(a, b):
len_a, len_b = len(a), len(b)
if len_a == len_b:
return binary_check_helper(a, b)
min_length, max_length = min(len_a, len_b), max(len_a, len_b)
res = binary_check_helper(a[:min_length], b[:min_length])
return res if res >= 0 else min_length
def binary_check_helper(a, b):
if a == b:
return -1
length = len(a)
if length == 1:
return int(a[0] == b[0])
else:
half_length = length // 2
r = binary_check_helper(a[:half_length], b[:half_length])
if r >= 0:
return r
r = binary_check(a[half_length:], b[half_length:])
if r >= 0:
return r + half_length
return r
In [34]: %timeit binary_check("a"*9999 + "b", "a"*9999 + "c")
10000 loops, best of 3: 28.4 µs per loop
这比genexp快了35倍以上!
为什么会这样呢?显然,比较操作需要线性时间,所以看起来我们做的工作比之前多得多...确实如此,但是它是在"C级别"完成的, 因此结果是这种方法实际上更快。
需要注意的是,这在某种程度上是“实现特定”的,因为像PyPy这样的实现可能会将genexp优化为单个C-for循环,并且这将超越任何东西; 而在像Jython或IronPython这样的实现中,速度可能比CPython慢得多。
该方法具有与其他方法相同的渐近复杂度,即O(n)。字符串最多分成一半log_2(n)
次,每次进行相等性测试,这需要线性时间。乍一看,它可能是一个Θ(n * logn)算法,但事实并非如此。递归方程为:
T(n) = T(n//2) + Θ(n) = Σ_{i=0}^{logn}(n/2^i)
= Θ(n(1 + 1/2 + 1/4 + ...)) <= Θ(2n) = Θ(n)
In [37]: %timeit binary_check("a"*10**6 + "b", "a"*10**6 + "c")
100 loops, best of 3: 2.84 ms per loop
In [38]: %timeit check_genexp("a"*10**6 + "b", "a"*10**6 + "c")
10 loops, best of 3: 101 ms per loop
In [39]: %timeit binary_check(15 * "a"*10**6 + "b", 15 * "a"*10**6 + "c")
10 loops, best of 3: 53.3 ms per loop
In [40]: %timeit check_genexp(15 * "a"*10**6 + "b", 15 * "a"*10**6 + "c")
1 loops, best of 3: 1.5 s per loop
您可以看到,即使是处理巨大字符串,这种方法仍然快大约30倍。
注意:该解决方案的缺点是它是ϴ(n)而不是O(n),也就是说,它总是需要读取整个字符串才能返回结果。即使第一个字符已经不同。实际上:
In [49]: a = "b" + "a" * 15 * 10**6
...: b = "c" + "a" * 15 * 10**6
...:
In [50]: %timeit binary_check(a, b)
100 loops, best of 3: 10.3 ms per loop
In [51]: %timeit check_genexp(a, b)
1000000 loops, best of 3: 1.3 µs per loop
这是可以预料的。然而,只需很少的改进,该解决方案就可以比显式循环更有效:
In [59]: a = "a" * 2 * 10**5 + "b" + "a" * 15*10**6
...: b = "a" * 2 * 10**5 + "c" + "a" * 15*10**6
In [60]: %timeit check_genexp(a, b)
10 loops, best of 3: 20.3 ms per loop
In [61]: %timeit binary_check(a, b)
100 loops, best of 3: 17.3 ms per loop
根据这个简单基准,如果大字符串的差异超过总长度的约1.3%,则二进制检查更好。
还可以引入一些启发式算法。例如,如果两个字符串的最小长度大于某个截止值,则首先仅检查该截止处的前缀是否不同,如果它们不同,则可以忽略其后的所有内容,从而避免比较整个字符串。这可以轻松地实现:
def binary_check2(a, b, cutoff=1000):
len_a, len_b = len(a), len(b)
if min(len_a, len_b) > cutoff:
small_a, small_b = a[:cutoff], b[:cutoff]
if small_a != small_b:
return binary_check_helper(a[:cutoff], b[:cutoff])
# same as before
genexp
方法。
在python3.4上执行时间。使用字节而不是Unicode字符串不会显著改变结果
for i, (x, y) in enumerate(zip(a, b)):
if x != y:
print('First index where strings are different:', i)
break
else:
print('Strings are identical.')
zip()
返回一个元组列表而不是迭代器。正如gnibbler所指出的那样,如果你在使用Python 2.x,使用izip
而不是zip
可能是值得的(izip
返回一个漂亮的、内存高效的迭代器,避免一次性评估所有值)。但是正如我在评论中所说的,在Python 3中,izip
已经被重命名为zip
,而旧的zip
已经不存在了。x,y
周围加上括号,因为 enumerate
会生成形式为 (idx,(aval,bval))
的元组。 - mgilsonizip
,因为字符串相当长,除非您预计差异总是接近结尾。 - John La Rooyzip==izip
。 - Joel Cornettfrom difflib import SequenceMatcher
s1 = 'stop at the bus'
s2 = 'stop at the car'
s = SequenceMatcher(None, s1, s2)
print s.get_matching_blocks()[0].size
from difflib import SequenceMatcher
s1 = 'stop at the bus'
s2 = 'stop at the car'
s = SequenceMatcher(None, s1, s2)
print s.get_matching_blocks()
[Match(a=0, b=0, size=12), Match(a=15, b=15, size=0)]
>>> s1 = 'stop at the bus'
>>> s2 = 'stop at the car'
>>> import difflib
>>> next(difflib.SequenceMatcher(a=s1, b=s2).get_matching_blocks())
Match(a=0, b=0, size=12)
这意味着第一个匹配块长度为12个字符。
如果a或b中任意一个不为0,则字符串从一开始就不同。
difflib
,所以很高兴看到这个例子。谢谢。(并+1 -- 我认为它相当干净 -- 当然不比next( ... enumerate(izip(s1,s2)) ...)
更糟) - mgilsonmatch.size if match.a==match.b==0 else 0
的操作。 - John La Rooy这可能有点过度,但既然您似乎关心速度,您可以考虑使用numpy。可能还有改进的空间(由于某种原因,内联对我产生了25微秒的差异),但这是第一步:
>>> def diff_index(s1, s2):
... s1 = numpy.fromstring(s1, dtype=numpy.uint8)
... s2 = numpy.fromstring(s2, dtype=numpy.uint8)
... return (~(s1 == s2)).nonzero()[0][0]
...
>>> base = string.lowercase * 385
>>> s1 = base + 'a'
>>> s2 = base + 'z'
>>> diff_index(s1, s2)
10010
针对结尾处的差异,这比genex快得多:
>>> %timeit next(idx for idx,(c1,c2) in enumerate(izip(s1,s2)) if c1 != c2)
1000 loops, best of 3: 1.46 ms per loop
>>> %timeit diff_index(s1, s2)
10000 loops, best of 3: 87.6 us per loop
在最开始的差异处速度会慢很多...
>>> s1 = 'a' + base
>>> s2 = 'z' + base
>>> %timeit next(idx for idx,(c1,c2) in enumerate(izip(s1,s2)) if c1 != c2)
100000 loops, best of 3: 2.12 us per loop
>>> %timeit diff_index(s1, s2)
10000 loops, best of 3: 87.5 us per loop
但平均而言,它的胜率高达一个数量级:
>>> s1 = base[:5000] + 'a' + base[5000:]
>>> s2 = base[:5000] + 'z' + base[5000:]
>>> %timeit next(idx for idx,(c1,c2) in enumerate(izip(s1,s2)) if c1 != c2)
1000 loops, best of 3: 724 us per loop
>>> %timeit diff_index(s1, s2)
10000 loops, best of 3: 87.2 us per loop
difflib
中,但我找不到。 - mgilson