Suppose I have a table like the following (This example is simplified, actual table is much larger), it is read from external files:
table = {{1, A, x, y},
{1, B, z, w},
{2, A, v, u},
{2, B, t, s}};
header = {i, j, g, h};
The third and fourth column are the actual content of my table, say their headers are g
and h
, and the first and second column are like indices for each row.
The output I am looking for is
tree = <|1 -> <|A -> <|g -> x, h -> y|>,
B -> <|g -> z, h -> w|>|>,
2 -> <|A -> <|g -> v, h -> u|>,
B -> <|g -> t, h -> s|>|>|>;
The reason behind this is that a regular table of association (give the first and second column header i
and j
) takes too long to find the rows using Select
(8800 sec for the actual table). This might speed up things quite a bit because it is a tree structure in nature. So if I want to access one particular row, I just use the command tree[1][A]
or tree[1][A][g]
to get the content of the first row.
So how would you code this into Mathematica? I am open to completely different approaches as well. Thanks!
<|g->h|>
but<g->x,h->y>
? I suppose in your real situation the list is quite large, so It's crucial to know about why you choose this way, or our answer will be useless for your real application. $\endgroup$g
andh
are just indices, the real data isx
andy
. $\endgroup$