I'm trying to learn more about list replacement without using Table
. I have a list like this:
list = {{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}}
Now, I want to modify the list elements, i.e. add the position of the respective elements in front of it and replace every element in the list with that:
listMod = {{{1->"a", 2->"b",3->"c"}, {1->"d", 2->"e", 3->"f"}, {1->"g", 2->"h", 3->"i"}}}
Answer
list = {{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}};
Map[MapIndexed[First@#2 -> #1 &], #, {2}] &@list
(* Out: {{{1 -> "a", 2 -> "b", 3 -> "c"}, {1 -> "d", 2 -> "e", 3 -> "f"},
{1 -> "g", 2 -> "h", 3 -> "i"}}} *)
Comments
Post a Comment