Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > lists with zero-valued elements

Reply
Thread Tools

lists with zero-valued elements

 
 
Nick Forest
Guest
Posts: n/a
 
      12-27-2003
Any body knows a trivial way to enumerate a list with n zero-valued elements?
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      12-27-2003

Nick> Any body knows a trivial way to enumerate a list with n
Nick> zero-valued elements?

I'm not sure what you're asking. I'd enumerate a list with n zero-valued
elements the same way I'd enumerate one with m zero-valued elements:

nelements = len(somelist)

If I wanted to count how many elements were zero, I might do something like:

nzeros = 0
for element in somelist:
if element == 0:
nzeros += 1

or more succinctly:

nzeros = len([element for element in somelist if element == 0])

Skip

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      12-27-2003
Nick Forest wrote:

> Any body knows a trivial way to enumerate a list with n zero-valued
> elements?


'enumerate'...? As in, enumerate(n*[0]) ... ?


Alex

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      12-27-2003
Skip Montanaro wrote:
...
> If I wanted to count how many elements were zero, I might do something
> like:
>
> nzeros = 0
> for element in somelist:
> if element == 0:
> nzeros += 1
>
> or more succinctly:
>
> nzeros = len([element for element in somelist if element == 0])


I'd do somelist.count(0) -- faster, more direct, and even more succint.


Alex

 
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
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
lists of lists Jon Slaughter C++ 4 12-13-2004 06:28 PM
Lists of Lists VWWall Computer Information 2 10-21-2004 01:15 AM
Sorting lists of lists... JustSomeGuy C++ 0 06-17-2004 05:44 PM



Advertisments