Optimize function similiar to dict.update() but adds common values
Gregory Piñero
gregpinero at gmail.com
Wed Dec 14 09:35:25 EST 2005
Hey guys,
I thought I'd throw this out there since everyone loves optimization
questions (it's true, check out the number of replies to those type of
questions!)
Right now I have a function shown below. I want to combine two
dictionaries and add the values together where-ever there is overlap.
I figured this function would be reasonably fast, but I have around 1
million keys in each dictionary (~80% overlap) and it just runs all
night.
So maybe my function is not O(n)? I really figured it was but maybe
dictionary lookups are O(nlogn)?
Anyway here is the function. Any ideas on doing this task a lot
faster would be appriciated.
Thanks!
<code apology=sorry if gmail kills my tabs>
def add_freqs(freq1,freq2):
"""Add two word freq dicts"""
newfreq={}
for key,value in freq1.items():
newfreq[key]=value+freq2.get(key,0)
for key,value in freq2.items():
newfreq[key]=value+freq1.get(key,0)
return newfreq
freq1={
'dog':1,
'cat':2,
'human':3
}
freq2={
'perro':1,
'gato':1,
'human':2
}
print add_freqs(freq1,freq2)
answer_I_want={
'dog':1,
'cat':2,
'perro':1,
'gato':1,
'human':5
}
</code>
--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
More information about the Python-list
mailing list