Velocity Reviews > how to break a for loop?

# how to break a for loop?

Gregory Petrosyan
Guest
Posts: n/a

 02-20-2006
Hello!
It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
that's why I encountered stupid problem: I need to remove zeros from
the begining of list, but I can't . I use

for i,coef in enumerate(coefs):
if coef == 0:
del coefs[i]
else:
break

but it removes ALL zeros from list. What's up?

P.S. I feel SO stupid asking this quastion...

Petr Jakes
Guest
Posts: n/a

 02-20-2006
zero_list=[0,0,0,0,0,1,2,3,4]
for x in range(len(zero_list)):
if zero_list[x]!=0: break
nonzero_list=zero_list[x:]
print nonzero_list

but some more "pythonic" solutions will be posted from other users I
guess

HTH
Petr Jakes

Jesus Rivero - (Neurogeek)
Guest
Posts: n/a

 02-21-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello Gregory!

I would also use lists to implement this. They are more practical and
you could take advantage of pop() or remove() methods instead of using
the Oh-so-Scary del. You could also use a lambda+map or reduce methods
to remove all zeros or just do something like this:

list_ = [0,0,0,1,0,0,1]
print [ elm for elm in list_ if elm != 0 ]
[1,1]

Regards,

Jesus (Neurogeek)

Gregory Petrosyan wrote:
> Hello!
> It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
> that's why I encountered stupid problem: I need to remove zeros from
> the begining of list, but I can't . I use
>
> for i,coef in enumerate(coefs):
> if coef == 0:
> del coefs[i]
> else:
> break
>
> but it removes ALL zeros from list. What's up?
>
>
> P.S. I feel SO stupid asking this quastion...
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFD+mVpdIssYB9vBoMRAl4hAJ9RnvgvEo5NsutG9KmD6q OEL7VyFgCfeLit
h7FLsbRvHR1z5DSxSPZHFlY=
=SY2U
-----END PGP SIGNATURE-----

Scott David Daniels
Guest
Posts: n/a

 02-21-2006
Gregory Petrosyan wrote:
> Hello!
> It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
> that's why I encountered stupid problem: I need to remove zeros from
> the begining of list, but I can't . I use
>
> for i,coef in enumerate(coefs):
> if coef == 0:
> del coefs[i]
> else:
> break

for index, coef in enumerate(coefs):
if coef:
if index:
del coefs[: index]
break

--Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)

=?ISO-8859-1?Q?Sch=FCle_Daniel?=
Guest
Posts: n/a

 02-21-2006
Gregory Petrosyan wrote:
> Hello!
> It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
> that's why I encountered stupid problem: I need to remove zeros from
> the begining of list, but I can't . I use
>
> for i,coef in enumerate(coefs):
> if coef == 0:
> del coefs[i]
> else:
> break
>
> but it removes ALL zeros from list. What's up?

I don't know how enumerate is implemented, but I would
suspect that modifying the list object in the loop
through del is asking for trouble

try
for i,coef in enumerate(coefs[:]):

> P.S. I feel SO stupid asking this quastion...

uda4i

hth, Daniel

=?ISO-8859-1?Q?Sch=FCle_Daniel?=
Guest
Posts: n/a

 02-21-2006
I just spend some more seconds thinking about it

lst = [0,0,0,1,2,3,0,11]

try:
del lst[0:lst.index(0)]
except ValueError:
pass

is better solution

bonono@gmail.com
Guest
Posts: n/a

 02-21-2006

Gregory Petrosyan wrote:
>I need to remove zeros from
> the begining of list, but I can't .

I believe the following is almost a direct translation of the above
sentence.

import itertools as it
a=[0,0,0,1,0]
a[:]=it.dropwhile(lambda x: x is 0, a)

Dennis Lee Bieber
Guest
Posts: n/a

 02-21-2006
On 20 Feb 2006 15:00:51 -0800, "Gregory Petrosyan"
<(E-Mail Removed)> declaimed the following in
comp.lang.python:

> Hello!
> It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
> that's why I encountered stupid problem: I need to remove zeros from
> the begining of list, but I can't . I use
>

Only from the beginning? (I think some of the other responses, which
I'd glanced over using Google-Groups at work, were stripping ALL zero
elements)...

May not be the most efficient, but it is definitely clear:

while len(coefs) > 1 and coefs[0] == 0:
coefs = coefs[1:]

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

Steven D'Aprano
Guest
Posts: n/a

 02-21-2006
Petr Jakes wrote:

> zero_list=[0,0,0,0,0,1,2,3,4]
> for x in range(len(zero_list)):
> if zero_list[x]!=0: break
> nonzero_list=zero_list[x:]
> print nonzero_list
>
> but some more "pythonic" solutions will be posted from other users I
> guess

That looks perfectly Pythonic to me.

You know, just because Python has for loops and
multi-line blocks of code doesn't mean we shouldn't use
them *wink*

--
Steven.

Giovanni Bajo
Guest
Posts: n/a

 02-21-2006
(E-Mail Removed) wrote:

>> I need to remove zeros from
>> the begining of list, but I can't .

>
> I believe the following is almost a direct translation of the above
> sentence.
>
> import itertools as it
> a=[0,0,0,1,0]
> a[:]=it.dropwhile(lambda x: x is 0, a)

Usa lambda x: x==0 or simply lambda x: x

Using 'is' relies on the interpreter reusing existing instances of the
immutable object '0', which is an implementation detail.
--
Giovanni Bajo