How to check for single character change in a string?

Arnaud Delobelle arnodel at gmail.com
Sat Dec 24 12:09:57 EST 2011


On 24 December 2011 16:10, Roy Smith <roy at panix.com> wrote:
> In article <roy-AAAEEA.10571424122011 at news.panix.com>,
>  Roy Smith <roy at panix.com> wrote:
>
>> >>> len([x for x in zip(s1, s2) if x[0] != x[1]])
>
> Heh, Ian Kelly's version:
>
>> sum(a == b for a, b in zip(str1, str2))
>
> is cleaner than mine.  Except that Ian's counts matches and the OP asked
> for non-matches, but that's an exercise for the reader :-)

Here's a variation on the same theme:

sum(map(str.__ne__, str1, str2))

-- 
Arnaud



More information about the Python-list mailing list