Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41971
| References | <fb693612-418f-459e-9141-6f837368cfd7@googlegroups.com> <CALWePYysMkLewOYw=kDLH=ezfiKkqeOW2Rxv8B1ciePWYkjk-A@mail.gmail.com> |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | 2013-03-27 01:08 +0000 |
| Subject | Re: how do you make a loop run in reverse? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3787.1364346554.2939.python-list@python.org> (permalink) |
> On 27 March 2013 10:59, <rahulreddy24@hotmail.com> wrote: >> >> So i have a set of for loops that create this : >> [snip] >> but i want to nest all the loops under one BIG loop that'll run in reverse >> to make this: [snip] >> Is this possible? On 27 March 2013 00:19, Xavier Ho <contact@xavierho.com> wrote: > There is a built-in function that reverses an iterable. Have a look at the > documentation. I assume you mean the reversed function. It does not in general reverse an iterable. From the docs: """ reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0). New in version 2.4. Changed in version 2.6: Added the possibility to write a custom __reversed__() method. """ So it reverses a sequence (having the __len__ and __getitem__ methods) or an object that advertises reversibility (having a __reversed__ method). It does not reverse anything else including generators, iterators and most non-sequence iterables. To the OP: To reverse a "set of for loops" as requested is not possible using the reversed function. It is, however, possible to reverse a list of strings. So if you have a function that returns the list of strings you show as output then you can easily reverse that list with reversed(mylist) or mylist[::-1]. Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how do you make a loop run in reverse? rahulreddy24@hotmail.com - 2013-03-26 16:59 -0700 Re: how do you make a loop run in reverse? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-27 00:18 +0000 Re: how do you make a loop run in reverse? Xavier Ho <contact@xavierho.com> - 2013-03-27 11:19 +1100 Re: how do you make a loop run in reverse? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-03-27 01:08 +0000 Re: how do you make a loop run in reverse? Dave Angel <davea@davea.name> - 2013-03-26 20:08 -0400 Re: how do you make a loop run in reverse? Arnaud Delobelle <arnodel@gmail.com> - 2013-03-27 21:18 +0000
csiph-web