Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of True Booleans in a Python List

I have a list of Booleans:

[True, True, False, False, False, True] 

and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have found examples of looking for the number of occurrences of specific elements, but is there a more efficient way to do it since I'm working with Booleans? I'm thinking of something analogous to all or any.

like image 968
acs Avatar asked Oct 07 '12 03:10

acs


People also ask

How do you count true numbers?

To count the number of TRUE entries, which here is 5, the formula =COUNTIF(A1:A6,TRUE) applied to the column should work, but it always returns the result 1. On the other hand, the formula =COUNTIF(A1:A6,FALSE) works correctly on a similar column got by pulling down FALSE.

How do I count the number of outputs in Python?

If you use Python Count() function to find the number of occurrences of '1' in that string, you get desired output within milliseconds. The python count() function is an inbuilt function of python which is used to count the number of occurrences of a character or substring in the string.


1 Answers

True is equal to 1.

>>> sum([True, True, False, False, False, True]) 3 
like image 169
Ignacio Vazquez-Abrams Avatar answered Oct 21 '22 18:10

Ignacio Vazquez-Abrams