-3

I am trying to calculate the number of characters that differ between two strings in python. Ideally I want a function just like strdif in C. I see ndiff in python's difflib, but that returns a Differ object whereas I want a simple integer (ex: "10011" vs "00110" returns 3). I know the answer must be simple, but I can't figure it out and Id rather use the library function instead of write it myself

  • 1
    Hi Noah. Consider taking the [tour](http://stackoverflow.com/tour) to get an idea of how to format your questions for the best results. Perhaps you should give your problem a shot and (potentially) ask a question when you run into a specific problem. – RyPope Sep 11 '14 at 20:39

1 Answers1

5
s1,s2 = "10011", "00110"
print sum(a!=b for a,b in zip(s1,s2)) + abs(len(s1)-len(s2))

should work fine.

or as John Clements points out

print sum(a!=b for a,b in map(None,s1,s2))

which avoids the extra length check ... and will be slightly faster if the strings are typically the same length (and its an awesome solution!) ...

or even more terse (now its starting to enter black magic land where enough reader comprehension is lost that I would probably not recommend actually implementing it like this in anything that may be seen by others, and if you do make sure to add lots of comments)

from operator import ne
print sum(map(ne, s1, s2))
Joran Beasley
  • 93,863
  • 11
  • 131
  • 160
  • 1
    If Python 2.x you can (ab)use `map`, eg: `sum(a != b for a, b in map(None, s1, s2))` to avoid the additional `len`, or use `sum(a != b for a, b in izip_longest(s1, s2, fillvalue=''))` as an alternative – Jon Clements Sep 11 '14 at 20:41
  • 1
    @JonClements: `+ abs(len(s1)-len(s2))` avoids unnecessary iteration when the string lengths differ. – Steven Rumbalski Sep 11 '14 at 20:44
  • heh both good points ... although I suspect the lengths will typically be the same ... – Joran Beasley Sep 11 '14 at 20:45
  • @StevenRumbalski there is that... I suppose it's the trade off between how often the strings are going to vary significantly in length – Jon Clements Sep 11 '14 at 20:46
  • I also wouldn't say "which is an awesome solution" - it's an *alternative* which has slightly different behaviour as per @Steven's comment :) – Jon Clements Sep 11 '14 at 20:47
  • 1
    (possibly even throw it down to all builtin level, eg: `from operator import ne; print sum(map(ne, s1, s2))` – Jon Clements Sep 11 '14 at 20:52