raise dict keys to the power of their value and multipy together quickly

301 views Asked by At

I'm trying to take a dictionary and (1) raise each key to the power of its value, and (2) multiply together the results of (1).

This needs to be done to several million rows of data. Any thoughts on how to speed up the process? I am using pandas data frame's map function to apply the function toTheExp to my column of data already. This step is still pretty slow though. Currently I'm trying something like:

import ast
from numpy import array
from functools import reduce
import operator

def toTheExp(d):
    return reduce(operator.mul, array(d.keys())**array(d.values()), 1)

toTheExp({2:3,4:5,6:7})
>>> 2293235712
2

There are 2 answers

2
jme On

Since you're importing numpy, I assume it's fine to use it over pandas.

def to_the_exp(d):
    x = np.fromiter(d.iterkeys(), np.float, count=len(d))
    p = np.fromiter(d.itervalues(), np.float, count=len(d))
    return np.prod(x**p)

so that:

>>> to_the_exp({2:3,4:5,6:7})
2293235712

A quick speed test with 10 million entries:

>>> %timeit to_the_exp(d)
1 loops, best of 3: 2.99 s per loop

and your code gives:

>>> %timeit toTheExp(d)
1 loops, best of 3: 7.96 s per loop

Your code isn't so slow, really. Perhaps the bottleneck is somewhere else? Also, the product of tens of millions of numbers is generally either zero, one, or infinity... Just want to make sure that you're doing something sensical here.

0
Marcin On

What about using comprehension, for example:

from functools import reduce
from operator import mul

print(reduce(mul,(k**v for k,v in {2:3,4:5,6:7}.items()),1)
#2293235712