472,348 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

FOR statement

Lad
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

Oct 20 '06 #1
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
Oct 20 '06 #2

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

Oct 20 '06 #3
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
Oct 20 '06 #4
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

Oct 20 '06 #5
Lad

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.

Oct 21 '06 #6
Ant

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.

Oct 21 '06 #7
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
Oct 21 '06 #8
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

Oct 21 '06 #9
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
Oct 21 '06 #10
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
Oct 21 '06 #11
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
Oct 21 '06 #12
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
Oct 21 '06 #13
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>

Oct 24 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
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...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
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: ...
37
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: ...
18
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: ...
28
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...
7
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...
19
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...
18
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...
23
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements....
0
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...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
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. ...
0
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...
0
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...
0
hi
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...
0
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...
0
Oralloy
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...
0
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....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.