How to count the number of true elements in a NumPy bool array

后端 未结 4 934
迷失自我
迷失自我 2020-12-12 13:50

I have a NumPy array \'boolarr\' of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 14:01

    That question solved a quite similar question for me and I thought I should share :

    In raw python you can use sum() to count True values in a list :

    >>> sum([True,True,True,False,False])
    3
    

    But this won't work :

    >>> sum([[False, False, True], [True, False, True]])
    TypeError...
    

提交回复
热议问题