Associations with a record structure (e.g., a flat table) can be organised in one of two main ways. First as a list of associations, which is also used in the case of Mathematica's data set functionality, such as
assoclist =
{<|"id" -> 1, "a1" -> 1, "a2" -> 2|>, <|"id" -> 2, "a1" -> 3, "a2" -> 4|>};
or as an association of associations with a particular key in this case the unique id of each record.
assoc2lev =
<| 1 -> <|"a1" -> 1, "a2" -> 2|>, 2 -> <|"a1" -> 3, "a2" -> 4|>|>;
For many operations both structures can be used. For instance, to retrieve a particular record with a given record id.
Query[Select[#id == 1 &]] @ assoclist
assoc2lev[1]
{<|"id" -> 1, "a1" -> 1, "a2" -> 2|>}
<|"a1" -> 1, "a2" -> 2|>
Or to select a specific element of the retrieved record
Query[Select[#id == 1 &], "a1"] @ assoclist // First
assoc2lev[1, "a1"]
1
1
While the Mathematica help pages on Dataset
are quite large, there are few examples regarding the use of association of associations. For simple retrievals the latter seems to be easier and more compact.
Are there any guidelines based on practical examples as to whether one form or other is better to use?
Comments
Post a Comment