get each pair from a string.

Vincent Davis vincent at vincentdavis.net
Sun Oct 21 20:33:55 EDT 2012


To All,
I appreciate the range of answers and the time each of you take to think
about and answer my question. Whether or not I use them I find them all
educational.
Thanks again.

Vincent



On Mon, Oct 22, 2012 at 2:03 AM, Emile van Sebille <emile at fenx.com> wrote:

> On 10/21/2012 12:06 PM, Ian Kelly wrote:
>
>> On Sun, Oct 21, 2012 at 12:58 PM, Vincent Davis
>> <vincent at vincentdavis.net> wrote:
>>
>>> x = 'apple'
>>> for f in range(len(x)-1):
>>>      print(x[f:f+2])
>>>
>>> @Ian,
>>> Thanks for that I was just looking in to that. I wonder which is faster I
>>> have a large set of strings to process. I'll try some timings if I get a
>>> chance later today.
>>>
>>
>> The solution you came up with is probably faster, but less general --
>> it will only work on sliceable sequences like strings, not arbitrary
>> iterables.
>>
>>
> So the simple loop is the right answer for sliceable sequences like
> strings, but not if your code needs to deal with arbitrary iterables such
> as those that the standard library authors are expected to handle.
>
> So, as OP's a self confessed newbie asking about slicing, why provide an
> example requiring knowledge of tee, enumerate, next and izip?
>
>
> def nwise(iterable, n=2):
>     iters = tee(iterable, n)
>     for i, it in enumerate(iters):
>         for _ in range(i):
>             next(it, None)
>     return izip(*iters)
>
> It's good that the standard library provides these tools as a convenience,
> but when all you need is a derringer, why reach for a howitzer?
>
> Emile
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121021/c8496e7b/attachment.html>


More information about the Python-list mailing list