[triangle-zpug] how to match strings in python

Chris Rossi chris at archimedeanco.com
Thu Apr 2 20:19:03 UTC 2009


On Thu, Apr 2, 2009 at 4:07 PM, Bradley A. Crittenden <
brad.crittenden at gmail.com> wrote:

>
> On Apr 2, 2009, at 15:27 , Chris Calloway wrote:
>
>  On 4/2/2009 2:15 PM, Jay Lyerly wrote:
>>
>>> For the python golfers.....
>>> a='foobar'
>>> b='foobaz'
>>> m=''
>>> for (x,y) in zip(a,b):  m+= x * (x==y)
>>> m contains the matching substring.  That's as close to one line as I
>>> could get.  :)
>>>
>>
>> Here it is in one line (synthesizing Jay and Chris Rossi's solution with a
>> list comprehension:
>>
>> >>> "".join([(x*(x==y)) for (x,y) in zip('foobar','foobaz')])
>> 'fooba'
>> >>>
>>
>
> Not enough testing!
>
> >>> "".join([(x*(x==y)) for (x,y) in zip('foobar','foobaz')])
> 'fooba'
> >>> "".join([(x*(x==y)) for (x,y) in zip('foobard','foobazd')])
> 'foobad'
>
>
> Dang, my pronouncement was premature.  I think Jay's would behave
similarly, too.

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://starship.python.net/pipermail/triangle-zpug/attachments/20090402/40e08c75/attachment.htm>


More information about the triangle-zpug mailing list