vim7: merging of dictionaries

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

vim7: merging of dictionaries

Mikołaj Machowski
Hello,

How to merge dictionaries?

I thought extend() would be best choice but when two keys are identical
values of one are overwritten.

How to sum values when keys are the same?

TIA

m.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: merging of dictionaries

Bram Moolenaar

Mikolaj Machowski wrote:

> How to merge dictionaries?
>
> I thought extend() would be best choice but when two keys are identical
> values of one are overwritten.
>
> How to sum values when keys are the same?

You would have to do it with a loop, something like:

        for k in keys(dict1)
            if has_key(dict2, k)
                let dict2[k] += dict1[k]
            else
                let dict2[k] = dict1[k]
            endif
        endfor

I don't think there is a generic solution for this.

--
   A cow comes flying over the battlements,  lowing aggressively.  The cow
   lands on GALAHAD'S PAGE, squashing him completely.
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: vim7: merging of dictionaries

Mikołaj Machowski
Dnia pi?tek, 16 wrze?nia 2005 10:46, Bram Moolenaar napisa?:

> Mikolaj Machowski wrote:
> > How to merge dictionaries?
> >
> > I thought extend() would be best choice but when two keys are
> > identical values of one are overwritten.
> >
> > How to sum values when keys are the same?
>
> You would have to do it with a loop, something like:
>
[cut]
>
> I don't think there is a generic solution for this.

Thanks.

m.