Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Deflatten the keys in a flat Association to create a nested Association
ResourceFunction["AssociationKeyDeflatten"][<|{k1,1,k1,2,…}→val1,{k2,1,k2,2,…}→val2,…|>] gives a nested Association assoc where each valn is found at assoc[kn,1,kn,2,…]. | |
ResourceFunction["AssociationKeyDeflatten"][assoc,f] uses f to interpret the keys of assoc as lists. |
Create a nested association from a flat association with lists as keys:
| In[1]:= |
| Out[1]= |
Keys not wrapped in a list are unchanged:
| In[2]:= |
| Out[2]= |
Provide a function to interpret keys as lists:
| In[3]:= |
| Out[3]= |
Values held by RuleDelayed will not be evaluated:
| In[4]:= |
| Out[4]= |
AssociationKeyDeflatten also works with lists of rules:
| In[5]:= |
| Out[5]= |
| In[6]:= |
| Out[6]= |
Deflatten the keys of an association:
| In[7]:= |
| Out[7]= |
| In[8]:= |
| Out[8]= |
Use AssociationKeyFlatten to get the original association:
| In[9]:= |
| Out[9]= |
If f and g are partial inverses of each other, AssociationKeyDeflatten[…,f] and AssociationKeyFlatten[…,g] typically will be as well:
| In[10]:= | ![]() |
| In[11]:= |
| Out[11]= |
| In[12]:= |
| Out[12]= |
| In[13]:= |
| Out[13]= |
| In[14]:= |
| Out[14]= |
Convert an association where keys are file paths into a nested association:
| In[15]:= | ![]() |
| Out[15]= | ![]() |
| In[16]:= |
| Out[16]= | ![]() |
Get everything under a particular directory:
| In[17]:= |
| Out[17]= |
Revert to the flat association:
| In[18]:= |
| Out[18]= | ![]() |
| In[19]:= |
| Out[19]= |
This work is licensed under a Creative Commons Attribution 4.0 International License