Merging dictionaries

aaron_watters at my-deja.com aaron_watters at my-deja.com
Thu Dec 16 14:53:15 EST 1999


In article <8391di$l4a$1 at nnrp1.deja.com>,
  Preston Landers <prestonlanders at my-deja.com> wrote:
>  Hello all,
>
> I've got a program that works with large data structures in the form
of
> nested dictionaries.
>
> The task at hand is to 'merge' two dictionaries with a special
> merge_function() called for items that exist in both dictionaries.

You really should consider using kjbuckets.kjDict's
(an alternate Python dictionary implementation) for this.
If you only want to call the merge_function where keys match
but values don't match then:

    def combine(d1, d2):
        "d1 and d2 should be kjDicts"
        from kjbuckets import kjSet
        test = d1+d2
        if test.Clean(): return test # no conflicts
        test.Wash()
        commonkeyset = kjSet(d1)&kjSet(d2)
        result = test - commonkeyset*test
        for ck in commonkeyset.items():
            result[ck] = merge_function(d1[ck], d2[ck])
        return result

alternatively if you always want to call the merge function
on common keys, even if the values match:

    def combine(d1, d2):
        "d1 and d2 should be kjDicts"
        from kjbuckets import kjSet
        test = d1+d2
        test.Wash()
        commonkeyset = kjSet(d1)&kjSet(d2)
        if not commonkeyset: return test # no conflicts
        result = test - commonkeyset*test
        for ck in commonkeyset.items():
            result[ck] = merge_function(d1[ck], d2[ck])
        return result

If you use the C extension module version this should be
much faster than the alternative implementation in the
average case (ie, unless the dictionaries have lots of
common keys).

Find out more about kjbuckets at http://www.chordate.com
   -- Aaron Watters

===
"So, what do you do?"
"I'm a puppeteer."
"Check, please!"   -- from "Being John Malkovich"


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list