Re: Alternative to standard C "for"

2005-02-17 Thread John Machin
James Stroud wrote: > > It seems I need constructs like this all of the time > > i = 0 > while i < len(somelist): > if oughta_pop_it(somelist[i]): > somelist.pop(i) > else: > i += 1 > > There has to be a better way... > ! for i in xrange(len(somelist)-1, -1, -1): ! if oughta_pop_it

Re: Alternative to standard C "for"

2005-02-17 Thread Jeremy Bowers
On Thu, 17 Feb 2005 10:22:01 -0800, James Stroud wrote: > It seems I need constructs like this all of the time > > i = 0 > while i < len(somelist): > if oughta_pop_it(somelist[i]): > somelist.pop(i) > else: > i += 1 > > There has to be a better way... > > Any thoughts? Others have p

Re: Alternative to standard C "for"

2005-02-17 Thread Kent Johnson
James Stroud wrote: It seems I need constructs like this all of the time i = 0 while i < len(somelist): if oughta_pop_it(somelist[i]): somelist.pop(i) else: i += 1 There has to be a better way... somelist[:] = [ item for item in somelist if not oughta_pop_it(item) ] Kent -- http://mail.

Re: Alternative to standard C "for"

2005-02-17 Thread Michael Spencer
James Stroud wrote: It seems I need constructs like this all of the time i = 0 while i < len(somelist): if oughta_pop_it(somelist[i]): somelist.pop(i) else: i += 1 There has to be a better way... Do you have to modify your list in place? If not, just create a copy with the filtered ite

Re: Alternative to standard C "for"

2005-02-17 Thread James Stroud
On Sat, 2005-02-05 at 10:49, BJörn Lindqvist wrote: > > I am quite new to Python, and have a straight & simple question. > > In C, there is for (init; cond; advance). We all know that. > > In Python there are two ways to loop over i=A..B (numerical.): > > 1) i = A > >while i > ...do somet

RE: Alternative to standard C "for"

2005-02-06 Thread Delaney, Timothy C (Timothy)
[EMAIL PROTECTED] wrote: > 2) for i in range(A, B, STEP): > ...do something... Note that the most common use of this is something like: t = 1, 2, 3 for i in range(len(t)): print i, t[i] This is best accomplished as: t = 1, 2, 3 for i, e in enumerate(t): p

Re: Alternative to standard C "for"

2005-02-05 Thread Georg Brandl
Alex Martelli wrote: > Georg Brandl <[EMAIL PROTECTED]> wrote: > >> Slight terminology glitch -- it does return an iterator, not a >> generator. Generators are functions that return iterators. > > xrange returns an ITERABLE, not an ITERATOR. Videat: > a = xrange(23, 43) a.next() > Tra

Re: Alternative to standard C "for"

2005-02-05 Thread Alex Martelli
Georg Brandl <[EMAIL PROTECTED]> wrote: > Slight terminology glitch -- it does return an iterator, not a > generator. Generators are functions that return iterators. xrange returns an ITERABLE, not an ITERATOR. Videat: >>> a = xrange(23, 43) >>> a.next() Traceback (most recent call last): Fil

Re: Alternative to standard C "for"

2005-02-05 Thread Diez B. Roggisch
> First case looks quite nasty, because it's for more complicated > things, not numerical loops. Second is very nice, but with there's > problem. If i do ..in range(1, 1).. (what I really need > sometimes), it takes few hundred megs of memory and slows > down. Are there other good ways for

Re: Alternative to standard C "for"

2005-02-05 Thread Georg Brandl
BJörn Lindqvist wrote: >> I am quite new to Python, and have a straight & simple question. >> In C, there is for (init; cond; advance). We all know that. >> In Python there are two ways to loop over i=A..B (numerical.): >> 1) i = A >>while i> ...do something... >> i+=STEP > > This

Re: Alternative to standard C "for"

2005-02-05 Thread Daniel Bickett
Paul Rubin wrote: > use xrange instead of range. Woops ;) I wasn't aware such a function existed. apologies-for-reinventing-the-wheel-ly y'rs, -- Daniel Bickett dbickett at gmail.com http://heureusement.org/ -- http://mail.python.org/mailman/listinfo/python-list

Re: Alternative to standard C "for"

2005-02-05 Thread Daniel Bickett
[EMAIL PROTECTED] wrote: > Are there other good ways for this simple problem? Generators? Very interesting problem :) That never occured to me. To prevent python from loading that entire list into memory, one could, as you suggested, use a generator: >>> def genrange( start , stop , step = 1 ):

Re: Alternative to standard C "for"

2005-02-05 Thread Paul Rubin
[EMAIL PROTECTED] writes: > problem. If i do ..in range(1, 1).. (what I really need > sometimes), it takes few hundred megs of memory and slows > down. Are there other good ways for this simple problem? Generators? use xrange instead of range. -- http://mail.python.org/mailman/listinfo/py

Re: Alternative to standard C "for"

2005-02-05 Thread BJörn Lindqvist
> I am quite new to Python, and have a straight & simple question. > In C, there is for (init; cond; advance). We all know that. > In Python there are two ways to loop over i=A..B (numerical.): > 1) i = A >while i ...do something... > i+=STEP This is indeed quite ugly. You rarely n

Alternative to standard C "for"

2005-02-05 Thread adomas . paltanavicius
Hi there, I am quite new to Python, and have a straight & simple question. In C, there is for (init; cond; advance). We all know that. In Python there are two ways to loop over i=A..B (numerical.): 1) i = A while ihttp://mail.python.org/mailman/listinfo/python-list