@Gregory Ewing: you were right, your version without *chain* is faster
and I quiet like it :)
```
from timeit import timeit
from itertools import chain, cycle, islice
def cycle_once_with_chain(sequence, start):
return chain(islice(sequence, start, None), islice(sequence,
start))
def cy
On Thu, Jun 1, 2017 at 4:14 AM, Gregory Ewing
wrote:
> guillaume.pau...@giome.fr wrote:
>>
>> def cycle_once(iterable, start):
>> return chain(islice(iterable, start, None), islice(iterable, start))
This assumes that iterable is restartable, which is not the case if
iterable is itself an iter
guillaume.pau...@giome.fr wrote:
def cycle_once(iterable, start):
return chain(islice(iterable, start, None), islice(iterable, start))
Another variation, maybe slightly more efficient:
from itertools import islice, cycle
def cycle_once(iterable, start):
return islice(cycle(iterable),
Il giorno mercoledì 31 maggio 2017 00:18:40 UTC+2, guillaum...@giome.fr ha
scritto:
> Hi Beppe !
>
> There are some powerful tools in the standard *itertools* module, you
> should have a look at it :)
> https://docs.python.org/3/library/itertools.html
>
> This is what I would do to cycle over y
Hi Beppe !
There are some powerful tools in the standard *itertools* module, you
should have a look at it :)
https://docs.python.org/3/library/itertools.html
This is what I would do to cycle over you iterable without making
several copies of it.
```
from itertools import islice, chain
def
c index
> with the difference that I would want to restart from the beginning when I
> reach the end of the tupla
>
>
> C
> D
> E
> F
> G
> H
> A
> B
>
> I would want to make a circular iteration
>
> suggestions?
As an alternative to the options m
pe "help", "copyright", "credits" or "license" for more information.
> >>>> x = ("A","B","C","D","E","F","G","H",)
> >>>> for i in x[2:]:
> >
ot;help", "copyright", "credits" or "license" for more information.
> >>>> x = ("A","B","C","D","E","F","G","H",)
> >>>> for i in x[2:]:
> > ...
thing like
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
x = ("A","B","C","D","E","F","G","H",
;,"B","C","D","E","F","G","H",)
>>>> for i in x[2:]:
> ... print i
> ...
> C
> D
> E
> F
> G
> H
>>>>
>
>
>
> with the difference that I would want to restart from th
016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = ("A","B","C","D","E","F","G","H",)
>&
Simon Brunning <[EMAIL PROTECTED]> wrote:
...
> > is there a faster way to build a circular iterator in python that by
doing this:
> >
> > c=['r','g','b','c','m','y','k']
> >
> > for i in range(30):
> > print c[i%len(c)]
>
> I don''t know if it's faster, but:
>
> >>> import itertools
> >
"Flavio codeco coelho" wrote:
> is there a faster way to build a circular iterator in python that by doing
> this:
>
> c=['r','g','b','c','m','y','k']
>
> for i in range(30):
>print c[i%len(c)]
have you benchmarked this, and found it lacking, or are you just trying
to optimize prematurely?
On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho <[EMAIL PROTECTED]> wrote:
> hi,
>
> is there a faster way to build a circular iterator in python that by doing
> this:
>
> c=['r','g','b','c','m','y','k']
>
> for i in range(30):
> print c[i%len(c)]
I don''t know if it's faster, but:
>>
Flavio codeco coelho wrote:
> hi,
>
> is there a faster way to build a circular iterator in python that by
> doing this:
>
> c=['r','g','b','c','m','y','k']
>
> for i in range(30):
> print c[i%len(c)]
>
> thanks,
>
> Flávio
>
>>> import itertools
>>> c=['r','g','b','c','m','y','k']
>>>
hi,
is there a faster way to build a circular iterator in python that by doing this:
c=['r','g','b','c','m','y','k']
for i in range(30):
print c[i%len(c)]
thanks,
Flávio
--
http://mail.python.org/mailman/listinfo/python-list
16 matches
Mail list logo