Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create an association indexed by a given key from a list of associations
ResourceFunction["KeyIndex"][{a1,a2,…},key] gives an Association that has keys ai[key] and values ai with key dropped. | |
ResourceFunction["KeyIndex"][key] represents an operator form of ResourceFunction["KeyIndex"] that can be applied to an expression. |
"DropKey" | True | whether to drop key from each of the ai |
MergingFunction | Automatic | determine how ai and aj should be combined when ai[key]⩵aj[key] |
MissingBehavior | Automatic | how to handle ai that do not have key |
Index a list of associations by a given key:
In[1]:= | ![]() |
Out[1]= | ![]() |
Index lists of rules:
In[2]:= | ![]() |
Out[2]= | ![]() |
Use a combination of lists and associations:
In[3]:= | ![]() |
Out[3]= | ![]() |
Use the operator form:
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
Index a Dataset by a given key:
In[6]:= | ![]() |
Out[6]= | ![]() |
In[7]:= | ![]() |
Out[7]= | ![]() |
The head of expr doesn’t necessarily have to be List:
In[8]:= | ![]() |
Out[8]= | ![]() |
If expr is an Association, KeyIndex uses Values[expr]:
In[9]:= | ![]() |
Out[9]= | ![]() |
Drop the given key from each of the resulting associations:
In[10]:= | ![]() |
Out[10]= | ![]() |
Leave the given key in the results:
In[11]:= | ![]() |
Out[11]= | ![]() |
By default, only the first value will be used when there is a collision:
In[12]:= | ![]() |
Out[12]= | ![]() |
Specify how collisions should be handled:
In[13]:= | ![]() |
Out[13]= | ![]() |
Combine values with Merge:
In[14]:= | ![]() |
Out[14]= | ![]() |
The default behavior of KeyIndex is to discard associations that lack the given key in the result:
In[15]:= | ![]() |
Out[15]= | ![]() |
Don’t apply special rules if the key is missing:
In[16]:= | ![]() |
Out[16]= | ![]() |
Create a new Dataset from an existing one that is indexed by a particular key:
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
Look up data using the new index:
In[19]:= | ![]() |
Out[19]= | ![]() |
Create groups by a given key:
In[20]:= | ![]() |
Out[20]= | ![]() |
Get the set matching a given classification:
In[21]:= | ![]() |
Out[21]= | ![]() |
KeyIndex[expr,key] is similar to GroupBy[expr,Key[key]]:
In[22]:= | ![]() |
Out[22]= | ![]() |
In[23]:= | ![]() |
Out[23]= | ![]() |
In[24]:= | ![]() |
Out[24]= | ![]() |
Similar behavior can be obtained by setting options:
In[25]:= | ![]() |
Out[25]= | ![]() |
The default behavior of KeyIndex does additional post-processing of the data:
In[26]:= | ![]() |
Out[26]= | ![]() |
In[27]:= | ![]() |
Out[27]= | ![]() |
Each item must be an Association or a List of rules:
In[28]:= | ![]() |
Out[28]= | ![]() |
In[29]:= | ![]() |
Out[29]= | ![]() |
In[30]:= | ![]() |
Out[30]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License