[triangle-zpug] how to match strings in python

Chris Calloway cbc at unc.edu
Thu Apr 2 16:02:16 UTC 2009


On 4/2/2009 11:31 AM, Chris Rossi wrote:
> match = []
> for c1,c2 in zip(string_1, string_2):
>    if c1 == c2:
>         match.append(c1)
>    else:
>         break
> match = "".join(match)

This is much better because it uses zip to iterate over two sequences at 
once. That's a Pythonic pattern to remember.

-- 
Sincerely,

Chris Calloway
http://www.secoora.org
office: 332 Chapman Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599






More information about the triangle-zpug mailing list