How to check for single character change in a string?

Roy Smith roy at panix.com
Sat Dec 24 10:57:14 EST 2011


In article <th7hs8-one.ln1 at chris.zbmc.eu>, tinnews at isbd.co.uk wrote:

> Can anyone suggest a simple/easy way to count how many characters have
> changed in a string?

Depending on exactly how you define "changed", you're probably talking 
about either Hamming Distance or Levenshtein Distance.  I would start 
with the wikipedia articles on both those topics and explore from there.

There are python packages for computing many of these metrics.  For 
example, http://pypi.python.org/pypi/python-Levenshtein/

> Is there any simpler/neater way than just a for loop running through
> both strings and counting non-matching characters?

If you don't care about insertions and deletions (and it sounds like you 
don't), then this is the way to do it.  It's O(n), and you're not going 
to get any better than that.  It's a one-liner in python:

>>> s1 = 'abcdefg'
>>> s2 = 'abcdekk'

>>> len([x for x in zip(s1, s2) if x[0] != x[1]])
2

But go read the wikipedia articles.  Computing distance between 
sequences is an interesting, important, and well-studied topic.  It's 
worth exploring a bit.



More information about the Python-list mailing list