If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
?
Thanks.
L 13 1302
Lad a écrit :
If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
?
Thanks.
L
for i in reversed(Mylist):
print i
Lad wrote:
If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
?
Thanks.
L
reverse the list in place with reverse method
l.reverse()
for i in l:
print i
and the reverse it back if needed
On 2006-10-20, Lad <py****@hope.czwrote:
If I have a list
Mylist=[1,2,3,4,5]
[...]
But how can I print it in a reverse order so that I get
5
4
3
2
1
Another option:
>>Mylist=[1,2,3,4,5] for i in Mylist[::-1]:
.... print i
....
5
4
3
2
1
But, I think the reversed(Mylist) way is better.
--
Grant Edwards grante Yow! Where's the Coke
at machine? Tell me a joke!!
visi.com
Lad wrote:
If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
>>def printreverse(lst):
if lst:
printreverse(lst[1:])
print lst[:1][0]
>>printreverse([1,2,3,4])
No good reason at all to do it this way. But recursion is fun.
-Jordan Greenberg
--
Posted via a free Usenet account from http://www.teranews.com wi******@hotmail.com wrote:
Lad wrote:
If I have a list
Mylist=[1,2,3,4,5]
I can print it
for i in Mylist:
print i
and results is
1
2
3
4
5
But how can I print it in a reverse order so that I get
5
4
3
2
1
?
Thanks.
L
reverse the list in place with reverse method
l.reverse()
for i in l:
print i
and the reverse it back if needed
Thank you ALL for help.
L.
Jordan Greenberg wrote:
....
>def printreverse(lst):
if lst:
printreverse(lst[1:])
print lst[:1][0]
Convoluted way of writing "print lst[0]" !
>printreverse([1,2,3,4])
No good reason at all to do it this way. But recursion is fun.
But there's a good reason not to. Try:
printreverse(range(1000))
Recursion has a maximum depth (of 1000 by default) in Python.
On 21 Oct 2006 00:50:34 -0700, Ant <an****@gmail.comwrote:
But there's a good reason not to. Try:
printreverse(range(1000))
Recursion has a maximum depth (of 1000 by default) in Python.
I guess Python isn't tail-recursive then?
Well, algorithms seem to be more naturally expressed iteratively in
Python, and to be fair, most uses of recursion you see in e.g., Scheme
textbooks are really just grandstanding in the real world.
-- Theerasak
Theerasak Photha:
I guess Python isn't tail-recursive then?
Right.
Well, algorithms seem to be more naturally expressed iteratively in
Python, and to be fair, most uses of recursion you see in e.g., Scheme
textbooks are really just grandstanding in the real world.
Still, some algorithms enjoy some recursivity anyway, like some graph
or tree exploration, structure flattening, and so on, for them I
sometimes use recursivity in Python too. The maximum recursivity level
can be increased too. Stackeless Python probably helps in recursive
code too. Psyco too. New Python versions have some optimizations for
function calling and so on, than help.
Often iterative code is the simpler solution, but sometimes recursivity
is the simpler solution, so a "better" (faster, leaner) recursivity
management may be good for a hi-level language like Python. So maybe in
future people here will improve its recursivity use some more.
Bye,
bearophile
On 21 Oct 2006 01:31:55 -0700, be************@lycos.com
<be************@lycos.comwrote:
Theerasak Photha:
I guess Python isn't tail-recursive then?
Right.
Well, algorithms seem to be more naturally expressed iteratively in
Python, and to be fair, most uses of recursion you see in e.g., Scheme
textbooks are really just grandstanding in the real world.
Still, some algorithms enjoy some recursivity anyway, like some graph
or tree exploration, structure flattening, and so on, for them I
sometimes use recursivity in Python too.
That's absolutely true. However, it is probably unusual in most
circumstances for such recursions to blow through more than a thousand
stack frames.
-- Theerasak
Theerasak Photha schrieb:
On 21 Oct 2006 00:50:34 -0700, Ant <an****@gmail.comwrote:
>But there's a good reason not to. Try:
printreverse(range(1000))
Recursion has a maximum depth (of 1000 by default) in Python.
I guess Python isn't tail-recursive then?
Nope. And given that you can decorate every function whenever you want ,
even at runtime, I can't see how that can be implemented easily - or at
all, to be honest.
Diez
Theerasak Photha schrieb:
On 21 Oct 2006 00:50:34 -0700, Ant <an****@gmail.comwrote:
>But there's a good reason not to. Try:
printreverse(range(1000))
Recursion has a maximum depth (of 1000 by default) in Python.
I guess Python isn't tail-recursive then?
To complement my other post: while it isn't tail recursive and can't be
so automatically, there do exist recipes to make certain functions tail
recursive by hand: http://aspn.activestate.com/ASPN/Coo.../Recipe/474088
Diez
On 10/21/06, Diez B. Roggisch <de***@nospam.web.dewrote:
Theerasak Photha schrieb:
On 21 Oct 2006 00:50:34 -0700, Ant <an****@gmail.comwrote:
But there's a good reason not to. Try:
printreverse(range(1000))
Recursion has a maximum depth (of 1000 by default) in Python.
I guess Python isn't tail-recursive then?
To complement my other post: while it isn't tail recursive and can't be
so automatically, there do exist recipes to make certain functions tail
recursive by hand:
http://aspn.activestate.com/ASPN/Coo.../Recipe/474088
Wow. That's 1337.
-- Theerasak
Michael Malinowski wrote:
Apologies if this is a stupidly obvious or simple question. If I have a
class with a series of attributes, is there a way to run a function
definition in the class whenever a specific attribute is changed?
you can implement a __setattr__ hook, or, in Python 2.2 and newer, use
properties: http://www.python.org/doc/2.2.3/what...00000000000000
note that "setter" properties only work if you inherit from "object".
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fábio Mendes |
last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with
things or the python-list sent my msg to /dev/null. I couldn't find...
|
by: Nerox |
last post by:
Hi, If i write:
#include <stdio.h>
int foo(int);
int main(void){
int a = 3;
foo(a);
}
|
by: eman1000 |
last post by:
I was recently looking at the prototype library
(http://prototype.conio.net/) and I noticed the author used the
following syntax:
...
|
by: Steven Bethard |
last post by:
The PEP below should be mostly self explanatory. I'll try to keep the
most updated versions available at:
...
|
by: Steven Bethard |
last post by:
I've updated the PEP based on a number of comments on comp.lang.python.
The most updated versions are still at:
...
|
by: Steven Bethard |
last post by:
Ok, I finally have a PEP number. Here's the most updated version of the
"make" statement PEP. I'll be posting it shortly to python-dev.
Thanks...
|
by: Steven Bethard |
last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The
patch is available at:
http://bugs.python.org/1472459
and I've pasted the full...
|
by: Steve |
last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement
I got ASP error number 13 when I use the SELECT...FOR UPDATE statement
as...
|
by: dspfun |
last post by:
Hi!
The words "expression" and "statement" are often used in C99 and C-
textbooks, however, I am not sure of the clear defintion of these
words...
|
by: florian.loitsch |
last post by:
According to the spec Section 14 the production
SourceElements:SourceElements SourceElement is evaluated as follows:
1. Evaluate SourceElements....
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
| |