Python 3 Most Performative Way to count elements in an array binned by f(element)

23 Views Asked by At

Given a function:

def foo_bar_bazzle(x):
   # do something, could be anything
   return result

and an array arr (1 <= len(arr) <= 10**9) of values to evaluate using that function, I want to find the most performative way of finding the count of elements, i in arr, grouped by the value of foo_bar_bazzle(i).

This gives me the correct grouped count:

rslt_i = {}
for i in arr:
     rslt_i.setdefault(foo_bar_bazzle(i), []).append(i)

allowing me to perform a non-associative operation on the len() of each foo_bar_bazzle(i):

return sum((len(v)**2 for v in rslt_i.values()))

But it isn't meeting my performance requirements. What can I do to speed up the count(x) group by f(x) here?

0

There are 0 best solutions below