Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to break a for loop?

Reply
Thread Tools

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...

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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-----
 
Reply With Quote
 
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

 
Reply With Quote
 
=?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[:]):
instead

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


uda4i

hth, Daniel

 
Reply With Quote
 
=?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

 
Reply With Quote
 
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)

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      02-21-2006
On 20 Feb 2006 15:00:51 -0800, "Gregory Petrosyan"
<> 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:]

--
> ================================================== ============ <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
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.

 
Reply With Quote
 
Giovanni Bajo
Guest
Posts: n/a
 
      02-21-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
break Wifi ? miro Wireless Networking 1 02-18-2005 12:44 AM
Did today's Windows updates break wireless?? The Celtic Warrior Wireless Networking 2 07-15-2004 10:41 PM
Word wrap line break code and algorithm for c# Jason Coyne Gaijin42 ASP .Net 0 04-08-2004 07:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57