Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Map a function conditionally over keys in an association
ResourceFunction["KeyMapIf"][f,test,assoc] applies f to only those keys ki in assoc for which test[ki] gives True. | |
ResourceFunction["KeyMapIf"][f,test] is the operator form of ResourceFunction["KeyMapIf"]. |
Map the function f over the keys of an Association, but only if the key is a string:
In[1]:= | ![]() |
Out[1]= | ![]() |
Turn any keys that are numeric in an Association into strings, using InputForm to show that it worked:
In[2]:= | ![]() |
Out[514]= | ![]() |
Use the operator form, using the resource function ShowQuotes to show that it worked:
In[515]:= | ![]() |
Out[515]= | ![]() |
Compare the output from an EntityValue operation before KeyMapIf is used with the much nicer output after being adjusted by KeyMapIf:
In[516]:= | ![]() |
Out[516]= | ![]() |
As with the resource function MapIf, KeyMapIf does not itself support level specifications. KeyMapIf can operate at deeper levels of an Association, however, through the use of Query:
In[517]:= | ![]() |
Out[517]= | ![]() |
KeyMapIf can work on an Association of associations contained in a Dataset (or even more deeply):
In[518]:= | ![]() |
Out[518]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License