473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1350
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 anything related in previous PEP's, so here it goes a...
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: Object.extend(MyObj.prototype, { my_meth1: function(){},...
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: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
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: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
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 again for the previous discussion and suggestions!...
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 text below. I've tried to be more explicit about...
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 below. However, if I use SELECT statement without...
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 with respect to C. Can somebody provide a sharp...
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. 2. If Result(1) is an abrupt completion, return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.