Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'counting': 0.09; 'match.': 0.09; 'subject:string': 0.09; 'am,': 0.12; '8:26': 0.16; 'simple:': 0.16; 'cc:addr:python-list': 0.16; 'received:74.125.82.44': 0.16; 'received:mail-ww0-f44.google.com': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'suggest': 0.20; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'changed': 0.23; 'cc:2**0': 0.24; 'subject:change': 0.24; 'sat,': 0.25; 'message- id:@mail.gmail.com': 0.28; '24,': 0.29; 'cc:addr:python.org': 0.29; 'subject:?': 0.31; 'anyone': 0.31; 'pretty': 0.32; 'there': 0.33; 'character': 0.34; 'loop': 0.34; 'received:74.125.82': 0.35; 'running': 0.35; 'subject:How': 0.35; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'characters': 0.39; 'e.g.': 0.39; 'should': 0.39; '2011': 0.61; 'results': 0.63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=Bz62PcbpvdACLrKOYGH/0TWKl7FUCJeA89of5Gmj3v0=; b=hZ98fMSy2XKnOvK2G9B0iNQWfm86SSljFAQKhJKBphNLniDi6H2wfwFzH83WwA8bIF 3F0d0GujXUSJvmFnF/2ZO4dBQn7RSyTlrZIRU/EtHDd8d0GIy/QhWxhzyxELD+4/R7qU 5b4TPCwjLvllEXpNnagqMKSZxXLTs9+vD5G58= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 24 Dec 2011 08:57:01 -0700 Subject: Re: How to check for single character change in a string? To: tinnews@isbd.co.uk Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324742254 news.xs4all.nl 6843 [2001:888:2000:d::a6]:49305 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17852 On Sat, Dec 24, 2011 at 8:26 AM, wrote: > Can anyone suggest a simple/easy way to count how many characters have > changed in a string? > > E.g. giving results as follows:- > > =A0 =A0abcdefg =A0 =A0 abcdefh =A0 =A0 =A0 =A0 1 > =A0 =A0abcdefg =A0 =A0 abcdekk =A0 =A0 =A0 =A0 2 > =A0 =A0abcdefg =A0 =A0 gfedcba =A0 =A0 =A0 =A0 6 > > > Note that position is significant, a character in a different position > should not count as a match. > > Is there any simpler/neater way than just a for loop running through > both strings and counting non-matching characters? No, but the loop approach is pretty simple: sum(a =3D=3D b for a, b in zip(str1, str2))