By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

FOR statement

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.