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
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
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.
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
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
[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
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
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
> 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
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
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
[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 ):
[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
> 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
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
15 matches
Mail list logo