Counting positive elements in a list with Python list comprehensions?

Sum(x > 0 for x in frequencies) bool is a subclass of int: In 15: isinstance(True,int) Out15: True and True's value is 1: In 16: True==1 Out16: True.

1 A variation: x > 0 for x in frequencies. Count(True) – Peter Jaric May 24 '10 at 20:38 @Peter: note that your suggestion loops twice over the data; once to build the output list, and twice to count True values. – tzot Jun 24 '10 at 19:16 @ΤΖΩΤΖΙΟΥ: Yes, of course, thanks for the info!

– Peter Jaric Jun 24 '10 at 19:20.

A slightly more Pythonic way would be to use a generator instead: sum(1 for x in frequencies if x > 0) This avoids generating the whole list before calling sum().

1 because this is a commonly overlooked way of doing a comprehension. If you're evaluating a list comprehension from within a function call, you can omit the . – jathanism May 24 '10 at 20:43 Breaks if none of the elements match the criteria.

– FogleBird Aug 8 '11 at 20:04 @FogleBird: the sum() of an empty generator returns 0. – Greg Hewgill Aug 8 '11 at 20:20 You're right. I got confused and was thinking of min() and max() – FogleBird Aug 8 '11 at 20:59.

This works, but adding bools as ints may be dangerous. Please take this code with a grain of salt (maintainability goes first): sum(k>0 for k in x).

1 Adding booleans as integers is guaranteed to work in Python 2 and 3: stackoverflow. Com/questions/2764017/… – EOL May 26 '10 at 7:30 +1 for the warning, though. :) – EOL May 26 '10 at 7:31.

You could use len() on the filtered list: len(x for x in frequencies if x > 0).

2 even better, to use a generator (strip and ) – valya May 24 '10 at 20:34 1 You could use filter with this to make it look more clear. Len(filter(lambda x: x > 0, frequencies)) – Jonathan Sternberg May 24 '10 at 20:35 1 @valya: That won't work with a generator – sth May 24 '10 at 20:50 @Jonathan: I'd say it's a matter of taste if you prefer filter() or a list comprehension, but usually list comprehensions are preferred to functional programming style. (And the OP asked for a list comprehension.) – sth May 24 '10 at 20:53 the OP actually only said (s)he is using a decent list comprehension right now, but didn't specifically ask for one.

But your main point still holds, of course. – Peter Jaric May 24 '10 at 21:02.

Reduce(lambda x, y: x+1 if y > 0 else x, frequencies) EDIT: With inspiration from the accepted answer from @~unutbu: reduce(lambda x, y: x + (y > 0), frequencies).

I wish I had got a comment to go with that down vote to learn by my mistakes. Please? – Peter Jaric May 24 '10 at 20:39 There seems to be a trend away from lambda functions toward list comprehensions.

– AntiRush May 28 '10 at 23:31 I wasn't one to downvote you; however I would gather that people tend to frown upon reduce, it being phased out etc (by Guido proclamation). I like reduce, but I too frown upon its use in this case, since the sum(x > 0…) variant seems more straightforward to me. – tzot Jun 24 '10 at 19:20.

If the array only contains elements >= 0 (i.e. All elements are either 0 or a positive integer) then you could just count the zeros and subtract this number form the length of the array: len(arr) - arr. Count(0).

A slightly more Pythonic way would be to use a generator instead.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions