I have just started to get used to Mathematica's Map, Apply, and related functions, and I can't figure out how to do maps that require working "one level down".
For example, I am currently trying to take a list of list of things, and basically perform a change of index.
index = {5, 3, 4, 1};
initial = {{1, 3}, {5, 4}, {5, 1}};
(*goal: {{4,2},{1,3},{1,4}}*)
To get the new value, I just have to take the value from initial
, and do Position[index, #1]
.
The problem I have is that
Position[index, #1] & /@ initial
Means that {1,3}
is passed as #1
, which is not what I want. I could use Flatten
to make the first part work, but I don't know how to "undo" the flatten to get back the original groupings.
Is there a way of dealing with these types of "expand list to elements" and the reverse operation when using Map/Apply
or am I just trying to use the wrong tool for the job (in which case what is the right tool)?
Alternative phrasing: Is there a way to apply a function to some structure (List
, List
of List
, etc) Mapping a function to all items with a Head
different from List
so that all the structure is preserved, but the values are changed?