Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20032
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; '__name__': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'am,': 0.12; 'def': 0.13; '"__main__":': 0.16; 'better?': 0.16; 'doctest': 0.16; 'reedy': 0.16; 'sequence,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'indices': 0.23; 'index': 0.24; "python's": 0.24; 'stuff': 0.26; 'import': 0.27; "i'm": 0.28; 'yield': 0.29; 'definition': 0.30; '"why': 0.30; "didn't": 0.30; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'header:X-Complaints-To:1': 0.34; 'running': 0.34; 'to:addr :python-list': 0.35; 'starting': 0.36; 'beginning': 0.36; 'received:org': 0.36; 'sequence': 0.37; 'but': 0.37; 'skip:" 10': 0.37; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'i.e.': 0.39; 'to:addr:python.org': 0.40; 'back': 0.60; 'kinds': 0.64; 'designed': 0.69; 'cycling': 0.84; 'nice,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Cycle around a sequence |
| Date | Wed, 08 Feb 2012 15:15:59 -0500 |
| References | <mailman.5525.1328663401.27778.python-list@python.org> <9pfeutFtjiU2@mid.individual.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-74-109-121-73.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 |
| In-Reply-To | <9pfeutFtjiU2@mid.individual.net> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5551.1328732195.27778.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1328732196 news.xs4all.nl 6876 [2001:888:2000:d::a6]:48416 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20032 |
Show key headers only | View raw
On 2/8/2012 9:25 AM, Neil Cerutti wrote:
> On 2012-02-08, Mark Lawrence<breamoreboy@yahoo.co.uk> wrote:
>> I'm looking at a way of cycling around a sequence i.e. starting
>> at some given location in the middle of a sequence and running
>> to the end before coming back to the beginning and running to
>> the start place. About the best I could come up with is the
>> following, any better ideas for some definition of better?
>
> Python's indices were designed for these kinds of shenanigans.
>
> def rotated(seq, n):
> """Iterate through all of seq, but starting from index n.
>
> >>> ", ".join(str(n) for n in rotated(range(5), 3))
> '3, 4, 0, 1, 2'
> """
>
> i = n - len(seq)
> while i< n:
> yield seq[i]
> i += 1
This is really nice, in the category of "Why didn't I think of that?"
(Probably because I knew the % mod solution from C and never 'updated'!)
> if __name__ == "__main__":
> import doctest
> doctest.testmod()
>
> If you have merely an iterable instead of a sequence, then look
> to some of the other clever stuff already posted.
To make a repeating rotator is only a slight adjustment:
k = n - len(seq)
while True:
i = k
while i < n:
yield seq[i]
i += 1
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Cycle around a sequence Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 01:10 +0000
Re: Cycle around a sequence Christoph Hansen <ch@radamanthys.de> - 2012-02-08 03:01 +0100
Re: Cycle around a sequence Neil Cerutti <neilc@norwich.edu> - 2012-02-08 14:25 +0000
Re: Cycle around a sequence Terry Reedy <tjreedy@udel.edu> - 2012-02-08 15:15 -0500
Re: Cycle around a sequence Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 22:47 +0000
Re: Cycle around a sequence Serhiy Storchaka <storchaka@gmail.com> - 2012-02-09 12:10 +0200
Re: Cycle around a sequence Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-09 03:55 +0000
Re: Cycle around a sequence Chris Angelico <rosuav@gmail.com> - 2012-02-09 15:16 +1100
Re: Cycle around a sequence Peter Otten <__peter__@web.de> - 2012-02-09 09:33 +0100
Re: Cycle around a sequence Chris Angelico <rosuav@gmail.com> - 2012-02-09 21:34 +1100
csiph-web