[Tutor] trouble with function-- trying to check differences btwn 2 strings
Luke Paireepinart
rabidpoobear at gmail.com
Tue Mar 6 18:50:33 CET 2007
zannah marsh wrote:
> what I was trying to do with that loop is check each character in the
> string against the corresponding character at the same position in the
> second string. rikart pointed out that my loop was actually checking
> if that character exists anywhere in the second string.
[snip the fix]
>
> and that's the fix. whether i am doing something totally weird, or
> that could be done a better way in python, i don't know...
The way you're doing it now is correct.
The function could be shortened to a single line, though, if you wanted to.
def stringdiff(string1,string2):
return [i for i in range(len(string1)) if string1[i] != string2[i]]
but I doubt your professor has covered list comprehensions yet, so the
way you solved the problem is most likely what he was looking for.
-Luke
More information about the Tutor
mailing list