2 added 912 characters in body
source | link
Clear[helper];
helper[tl_] := GroupBy[tl, First -> Rest, helper];
helper[{{n_}}] := n;
helper@Transpose@Append[Flatten@vals]@Transpose@Tuples[dims]

The idea is to build a list of paths to each leaf, then use a recursive GroupBy build the association tree from the list.

Update

I have managed to come up with something faster. But the code becomes more ugly.

Clear[fvals, i, maketree];
fvals = Flatten[vals, Depth[vals] - 3];
maketree[{first_, rest__}] := 
  AssociationThread[first -> Table[maketree[{rest}], Length@first]];
maketree[{first_}] := AssociationThread[first, fvals[[i++]]];

i = 1; maketree[dims]
(*<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
        0., "2017" -> 633.25|>, 
     "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
       "2017" -> 632.45|>, 
     "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>*)

I made some timing experiments on code available. (1000 times, repeated timing).

My previous: 0.0665s

My current: 0.019s

Kuba: 0.0400s

andre: 0.0299s

Anton: 0.307s

Clear[helper];
helper[tl_] := GroupBy[tl, First -> Rest, helper];
helper[{{n_}}] := n;
helper@Transpose@Append[Flatten@vals]@Transpose@Tuples[dims]

The idea is to build a list of paths to each leaf, then use a recursive GroupBy build the association tree from the list.

Clear[helper];
helper[tl_] := GroupBy[tl, First -> Rest, helper];
helper[{{n_}}] := n;
helper@Transpose@Append[Flatten@vals]@Transpose@Tuples[dims]

The idea is to build a list of paths to each leaf, then use a recursive GroupBy build the association tree from the list.

Update

I have managed to come up with something faster. But the code becomes more ugly.

Clear[fvals, i, maketree];
fvals = Flatten[vals, Depth[vals] - 3];
maketree[{first_, rest__}] := 
  AssociationThread[first -> Table[maketree[{rest}], Length@first]];
maketree[{first_}] := AssociationThread[first, fvals[[i++]]];

i = 1; maketree[dims]
(*<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
        0., "2017" -> 633.25|>, 
     "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
       "2017" -> 632.45|>, 
     "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>*)

I made some timing experiments on code available. (1000 times, repeated timing).

My previous: 0.0665s

My current: 0.019s

Kuba: 0.0400s

andre: 0.0299s

Anton: 0.307s

1
source | link

Clear[helper];
helper[tl_] := GroupBy[tl, First -> Rest, helper];
helper[{{n_}}] := n;
helper@Transpose@Append[Flatten@vals]@Transpose@Tuples[dims]

The idea is to build a list of paths to each leaf, then use a recursive GroupBy build the association tree from the list.