Re: for statement on empty iterable

2007-08-22 Thread J. Cliff Dyer
Neil Cerutti wrote: On 2007-08-22, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: While it is desireable to not only write working, but also aesthetically pleasing code, as a beginner you shouldn't worry too much. The sense of aesthetics develops with time. Important is to try and grasp the idio

Re: for statement on empty iterable

2007-08-22 Thread Neil Cerutti
On 2007-08-22, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > While it is desireable to not only write working, but also > aesthetically pleasing code, as a beginner you shouldn't worry > too much. The sense of aesthetics develops with time. Important > is to try and grasp the idioms of the language

Re: for statement on empty iterable

2007-08-22 Thread Steve Holden
Amit Khemka wrote: > On 8/22/07, james_027 <[EMAIL PROTECTED]> wrote: >> hi Paul, >> >>> That doesn't crash or anything like that, but it also doesn't >>> set the index variable, which can cause confusion in some situations. >> Thanks for your quick answer ... Actually I was thinking how do I >> ac

Re: for statement on empty iterable

2007-08-22 Thread Eric Abrahamsen
Here's another simple method: l = ['j', 'a', 'm', 'e', 's'] counter = 0 for i in l: # Do your code counter += 1 print counter Yrs, Eric > l = ['j', 'a', 'm', 'e', 's'] > > for i in l > # i want to know the nth number of times it has loop thru or > something like counter? > > Thanks

Re: for statement on empty iterable

2007-08-22 Thread Diez B. Roggisch
james_027 schrieb: > hi, > >> Oh I see. You have to combine a couple of concepts but for this >> example you'd say: >> >>name = 'james' # 'l' looks too much like the digit 1 >>for i,c in enumerate(name): >> print i, c >>print i >> >> enumerate(name) generates the sequence >>

Re: for statement on empty iterable

2007-08-21 Thread james_027
hi, > Oh I see. You have to combine a couple of concepts but for this > example you'd say: > >name = 'james' # 'l' looks too much like the digit 1 >for i,c in enumerate(name): > print i, c >print i > > enumerate(name) generates the sequence > >(0,'j'), (1,'a'), (2,'m'),

Re: for statement on empty iterable

2007-08-21 Thread Paul Rubin
james_027 <[EMAIL PROTECTED]> writes: > Yes i am new to python :). I am sorry I should be clarify myself ... > for example > > l = ['j', 'a', 'm', 'e', 's'] > > for i in l > # i want to know the nth number of times it has loop thru or > something like counter? Oh I see. You have to combine a c

Re: for statement on empty iterable

2007-08-21 Thread Amit Khemka
On 8/22/07, james_027 <[EMAIL PROTECTED]> wrote: > hi Paul, > > > > > That doesn't crash or anything like that, but it also doesn't > > set the index variable, which can cause confusion in some situations. > > Thanks for your quick answer ... Actually I was thinking how do I > access the index insi

Re: for statement on empty iterable

2007-08-21 Thread james_027
hi, > It sounds like you're just starting to learn the language... have you > read the online tutorial yet? That is a pretty easy introduction. > > See:http://python.org/doc/ > > Anyway, you can say > >for i in (1,2,3): > print i*5 > > to print 5, 10, and 15 on separate lines, for examp

Re: for statement on empty iterable

2007-08-21 Thread Paul Rubin
james_027 <[EMAIL PROTECTED]> writes: > Thanks for your quick answer ... Actually I was thinking how do I > access the index inside a for statement? Can you help It sounds like you're just starting to learn the language... have you read the online tutorial yet? That is a pretty easy introduction.

Re: for statement on empty iterable

2007-08-21 Thread james_027
hi Paul, > > That doesn't crash or anything like that, but it also doesn't > set the index variable, which can cause confusion in some situations. Thanks for your quick answer ... Actually I was thinking how do I access the index inside a for statement? Can you help Thanks james -- http://mai

Re: for statement on empty iterable

2007-08-21 Thread Paul Rubin
james_027 <[EMAIL PROTECTED]> writes: > for i in []: > #do something > is this safe? or should I put a if statement to test it first? That doesn't crash or anything like that, but it also doesn't set the index variable, which can cause confusion in some situations. -- http://mail.python.org/mail

Re: FOR statement

2006-10-21 Thread Theerasak Photha
On 10/21/06, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > Theerasak Photha schrieb: > > On 21 Oct 2006 00:50:34 -0700, Ant <[EMAIL PROTECTED]> wrote: > > > >> But there's a good reason not to. Try: > >> > >> printreverse(range(1000)) > >> > >> Recursion has a maximum depth (of 1000 by default) in

Re: FOR statement

2006-10-21 Thread Diez B. Roggisch
Theerasak Photha schrieb: > On 21 Oct 2006 00:50:34 -0700, Ant <[EMAIL PROTECTED]> wrote: > >> 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

Re: FOR statement

2006-10-21 Thread Diez B. Roggisch
Theerasak Photha schrieb: > On 21 Oct 2006 00:50:34 -0700, Ant <[EMAIL PROTECTED]> wrote: > >> 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

Re: FOR statement

2006-10-21 Thread Theerasak Photha
On 21 Oct 2006 01:31:55 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > 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., Sc

Re: FOR statement

2006-10-21 Thread bearophileHUGS
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

Re: FOR statement

2006-10-21 Thread Theerasak Photha
On 21 Oct 2006 00:50:34 -0700, Ant <[EMAIL PROTECTED]> wrote: > 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 it

Re: FOR statement

2006-10-21 Thread 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

Re: FOR statement

2006-10-21 Thread Lad
[EMAIL PROTECTED] 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 >

Re: FOR statement

2006-10-20 Thread Jordan Greenberg
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: printrev

Re: FOR statement

2006-10-20 Thread Grant Edwards
On 2006-10-20, Lad <[EMAIL PROTECTED]> wrote: > 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(

Re: FOR statement

2006-10-20 Thread [EMAIL PROTECTED]
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 meth

Re: FOR statement

2006-10-20 Thread Christophe
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):