'Hi there , I got stuck on this silly step. I need to count how many heads I have int his array

I needed to convert an array of coin tosses into a binary array. For example, morph H into 0 and T into 1. The problem is that I am trying to use .count but it does not seem to work. Also I will need to find the #number of max running heads. Could someone explain me how to do that. Thanks in advance.This is the code:

import numpy as np
sample = np.load("sample_1.npy")#search for how to load a ".npy" file

sample_arr = np.asarray(sample)#convert 'sample' to a binary array
sample_converted = np.where(sample_arr == 'H', 0, 1)
print(sample_converted)

n = len(sample_converted) #total coin tosses
print(n)

heads_total = sample_arr.count("H") #number of heads
print(heads_total)



Solution 1:[1]

I solved the first step of counting heads:

heads_total = np.count_nonzero(sample_converted == 0) #number of heads print(heads_total)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1