Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41964 > unrolled thread
| Started by | rahulreddy24@hotmail.com |
|---|---|
| First post | 2013-03-26 16:59 -0700 |
| Last post | 2013-03-27 21:18 +0000 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | rahulreddy24@hotmail.com |
|---|---|
| Date | 2013-03-26 16:59 -0700 |
| Subject | how do you make a loop run in reverse? |
| Message-ID | <fb693612-418f-459e-9141-6f837368cfd7@googlegroups.com> |
So i have a set of for loops that create this :
***************************************
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
***************************************
*
***
*****
*******
*********
but i want to nest all the loops under one BIG loop that'll run in reverse to make this:
***************************************
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
***************************************
*
***
*****
*******
*********
*******
*****
***
*
***************************************
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
*** *** *** *** *** *** ***
***************************************
Is this possible?
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-03-27 00:18 +0000 |
| Message-ID | <mailman.3783.1364343375.2939.python-list@python.org> |
| In reply to | #41964 |
On 26/03/2013 23:59, rahulreddy24@hotmail.com wrote: > So i have a set of for loops that create this : > snipped the art > > but i want to nest all the loops under one BIG loop that'll run in reverse to make this: > snipped the art > > Is this possible? > Yes read http://docs.python.org/3/library/stdtypes.html#range and http://docs.python.org/3/library/functions.html#reversed -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Xavier Ho <contact@xavierho.com> |
|---|---|
| Date | 2013-03-27 11:19 +1100 |
| Message-ID | <mailman.3785.1364343607.2939.python-list@python.org> |
| In reply to | #41964 |
[Multipart message — attachments visible in raw view] — view raw
There is a built-in function that reverses an iterable. Have a look at the documentation. xav On 27 March 2013 10:59, <rahulreddy24@hotmail.com> wrote: > So i have a set of for loops that create this : > > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > * > *** > ***** > ******* > ********* > > but i want to nest all the loops under one BIG loop that'll run in reverse > to make this: > > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > * > *** > ***** > ******* > ********* > ******* > ***** > *** > * > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > Is this possible? > -- > http://mail.python.org/mailman/listinfo/python-list >
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-03-27 01:08 +0000 |
| Message-ID | <mailman.3787.1364346554.2939.python-list@python.org> |
| In reply to | #41964 |
> 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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-03-26 20:08 -0400 |
| Message-ID | <mailman.3812.1364402854.2939.python-list@python.org> |
| In reply to | #41964 |
On 03/26/2013 07:59 PM, rahulreddy24@hotmail.com wrote: > So i have a set of for loops that create this : > > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > * > *** > ***** > ******* > ********* > > but i want to nest all the loops under one BIG loop that'll run in reverse to make this: > > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > * > *** > ***** > ******* > ********* > ******* > ***** > *** > * > *************************************** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *** *** *** *** *** *** *** > *************************************** > > Is this possible? > tes -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2013-03-27 21:18 +0000 |
| Message-ID | <mailman.3829.1364419117.2939.python-list@python.org> |
| In reply to | #41964 |
On 26 March 2013 23:59, <rahulreddy24@hotmail.com> wrote:
> So i have a set of for loops that create this :
>
> ***************************************
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> ***************************************
>
> *
> ***
> *****
> *******
> *********
>
> but i want to nest all the loops under one BIG loop that'll run in reverse to make this:
>
> ***************************************
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> ***************************************
>
> *
> ***
> *****
> *******
> *********
> *******
> *****
> ***
> *
> ***************************************
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> *** *** *** *** *** *** ***
> ***************************************
>
> Is this possible?
Let's have a look at a simple example. Imagine you have a function:
>>> def print_pretty_pattern():
... for i in range(1, 6):
... print '*'*i
...
>>> print_pretty_pattern()
*
**
***
****
*****
You can't reverse the pattern because it's not data so you need to
turn it into data:
>>> def generate_pretty_pattern():
... for i in range(1, 6):
... yield '*'*i
It's the same as above, but the 'print' statement has been replace
with a 'yield' statement, making the function into a generator
function, so that when you call it you get an iterable of all the
lines. You can now make a function to print a pattern:
>>> def print_pattern(lines):
... for line in lines:
... print line
Or if you want to be concise:
>>> def print_pattern(lines):
... print "\n".join(lines)
So you can print any pattern:
>>> print_pattern(generate_pretty_pattern())
*
**
***
****
*****
So now you can write another generator that makes the mirror pattern
of a given pattern:
>>> def mirror_pattern(pattern):
... lines = []
... for line in pattern:
... yield line
... lines.append(line)
... if lines:
... lines.pop() # don't repeat the last line
... for line in reversed(lines):
... yield line
...
>>> print_pattern(mirror_pattern(generate_pretty_pattern()))
*
**
***
****
*****
****
***
**
*
Here's another example:
>>> print_pattern(mirror_pattern(''.join(mirror_pattern("*".ljust(i).rjust(15))) for i in range(1,16,2)))
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
--
Arnaud
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web