Is there a better way to count the number of ones in a binary number when it is stored in base 10 using python? I have a large array of unsigned ints that are each associated with a mapping of active edges on a graph. I'd like to count the number of active edges, e.g. when the number is converted to binary, how many "1's" are there in the result? The minimal working example is below, but I'm sure it can be done without resorting to a string casting.
A = [0,1,33,2,18,4,12,8,16,32,]
bin_str = '{0:0b}'
B = [bin_str.format(a) for a in A]
count = [len([c for c in b if c=='1']) for b in B]
print A
print B
print count
> [0, 1, 33, 2, 18, 4, 12, 8, 16, 32]
> ['0', '1', '100001', '10', '10010', '100', '1100', '1000', '10000', '100000']
> [0, 1, 2, 1, 2, 1, 2, 1, 1, 1]
Edit: The related question hints at a lookup table, and something that relies on magic bits. It is unclear if those solutions will work for a 64-bit integer (which is the known max for this problem), nor are they python specific.