get each pair from a string.

Daniel Nogues dnogues_mc at hotmail.com
Mon Oct 22 14:17:03 EDT 2012


Hello rusi

This is a little bit faster:

s = "apple"
[s[i:i+2] for i in range(len(s)-1)]


 >>> timeit("""s = "apple"
... [a+b for a,b in zip(s, s[1:])]""",number=10000)
0.061038970947265625

 >>> timeit("""s = "apple"
... [s[i:i+2] for i in range(len(s)-1)]""",number=10000)
0.0467379093170166

Regards

> From: rusi <rustompmody at gmail.com>
> Date: 22 October 2012 17:19:35 IST
> To: python-list at python.org
> Subject: Re: get each pair from a string.
>
>
> On 10/21/2012 11:33 AM, Vincent Davis wrote:
>
>> I am looking for a good way to get every pair from a string. For  
>> example,
>> input:
>> x = 'apple'
>> output
>> 'ap'
>> 'pp'
>> 'pl'
>> 'le'
>
> Maybe zip before izip for a noob?
>
>>>> s="apple"
>>>> [a+b for a,b in zip(s, s[1:])]
> ['ap', 'pp', 'pl', 'le']
>>>>
>
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121022/e3d63d24/attachment.html>


More information about the Python-list mailing list