3
$\begingroup$

I have the following two associations

<|A->2.02015,B->1.98025|> <|B->0.538000,A->0.462000|> 

I would like to produce a third association where

new = <|A-> 2.02015/0.462000, B->1.98025/0.538000|> 

I found the following answer which is close, except it uses Total. Is there an equivalent for quotients (Quotient does not work)? Or is there a better way to do it?

Association Sum by key

$\endgroup$

3 Answers 3

7
$\begingroup$
a = <|A -> 2.02015, B -> 1.98025|>; b = <|B -> 0.538000, A -> 0.462000|>; Merge[{a, b}, Apply[Divide]] 
$\endgroup$
5
$\begingroup$

If you do not want to use builtin commands like Merge you could always roll your own function to do it.

a = <|A -> 2.02015, B -> 1.98025|> b = <|B -> 0.538000, A -> 0.462000|> keys = Keys[a]; 

Mathematica graphics

Now simply build your new association using pure function like this

<| # -> (a[#]/b[#]) & /@ keys|> 

Mathematica graphics

Compare to

new = <|A-> 2.02015/0.462000, B->1.98025/0.538000|> 

Mathematica graphics

$\endgroup$
1
$\begingroup$
a = <|A -> 2.02015, B -> 1.98025|>; b = <|B -> 0.538000, A -> 0.462000|>; 

In the above case it's simply

a / KeySort[b] 

enter image description here

If we don't know the sort status:

KeySort[a] / KeySort[b] 

enter image description here

Or, alternatively,

Divide @@ Map[KeySort, {a, b}] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.