Wolfram: Map Function to List
Map
Map[f, list]-
🔸 SHORT SYNTAX:
f /@ listApply a function to each element, return the result list.
(* Map a function to list *) Map[ f, {3,4,5} ] (* {f[3], f[4], f[5]} *) (* short syntax *) f /@ {3,4,5} (* Map a user-defined function to list *) Map[ Function[x, x+1 ], {1, 2, 3} ] (* {2, 3, 4} *)
Map to Nodes by Specified Depth Levels
map a function to all leaf nodes, or all nodes at the same level, or range of levels.
Map[f, list, levelSpec]-
Apply a function to nodes of tree, at Level Spec .
💡 TIP: one great use is map to leafs of a tree by using
{-1}for levelSpec.(* map to first level *) Map[ f, {1, {2, {3}}}, {1} ] (* {f[1], f[{2, {3}}]} *) (* map to second level *) Map[ f, {1, {2, {3}}}, {2} ] (* {1, {f[2], f[{3}]}} *) (* map to third level *) Map[ f, {1, {2, {3}}}, {3} ] (* {1, {2, {f[3]}}} *) (* map to leafs *) Map[ f, {1, {2, {3}}}, {-1} ] (* {f[1], {f[2], {f[3]}}} *) Partition[Range[6], 2] (* {{1, 2}, {3, 4}, {5, 6}} *) (* typical use. Map to elements in matrix *) Map[ f, {{1, 2}, {3, 4}, {5, 6}}, {2} ] (* {{f[1], f[2]}, {f[3], f[4]}, {f[5], f[6]}} *)
Map with Index
when you map a function to list, sometime you want the position of the element fed to the function.
MapIndexed-
MapIndexed[ f, list ]MapIndexed[ f, list, levelSpec ]→ map at Level Spec
Like
Map, but also feed the index to the function.💡 TIP: Useful when you want to go over a list but also need the index.
The function receive 2 args, first is value, second is the Position spec of the value.
MapIndexed[ f, {a,b,c} ] (* {f[a, {1}], f[b, {2}], f[c, {3}]} *) (* map at level 2 *) MapIndexed[ f, {{a}, {b}}, {2} ] (* {{f[a, {1, 1}]}, {f[b, {2, 1}]}} *) (* map at level -1 *) MapIndexed[ f, {{a}, {b}, c}, {-1} ] (* {{f[a, {1, 1}]}, {f[b, {2, 1}]}, f[c, {3}]} *)
Map to Multiple Lists
MapThread-
MapThread[ f, {list1 , list2, etc} ]MapThread[ f, {list1 , list2, etc}, n ]→ map at Level n
Like
Map, but for functions that take 2 args (or more). The function is given args from each list.MapThread[ f, {{1, 2, 3} , {a, b, c}} ] (* {f[1, a], f[2, b], f[3, c]} *) MapThread[f, {{1, 2, 3}, {a, b, c}, {x, y, z}}] (* {f[1, a, x], f[2, b, y], f[3, c, z]} *) (* map to level 2 *) MapThread[ f, {{{1}, {2}} , {{a}, {b}}} , 2 ] (* {{f[1, a]}, {f[2, b]}} *)
Map at Specific Nodes by Their Positions
MapAt-
MapAt[f, list, n]→ change at index nMapAt[f, list, positionSpec]→ change an element at Position specMapAt[f, list, positionsList]→ change at multiple positions.
change specific elements in a list.
(* change one item. specified by index. *) MapAt[f, {1, {a,b}, 3}, 3] (* {1, {a, b}, f[3]} *) (* HHHH------------------------------ *) (* change one item, at position {3} *) MapAt[f, {1, {a,b}, 3}, {3}] (* {1, {a, b}, f[3]} *) (* change one item, at position {2,1} *) MapAt[f, {1, {a,b}, 3}, {2,1}] (* {1, {f[a], b}, 3} *) (* HHHH------------------------------ *) (* change multiple items, specified by a list of their positions *) MapAt[f, {1, {a,b}, 3}, {{2},{3}}] (* {1, f[{a, b}], f[3]} *)
BlockMap (Map at Neighboring Groups)
map to list or matrix by blocks of elements at a time.
BlockMap-
BlockMap[f,list,n]→ n at a time.BlockMap[f,list,n,d]→ with d offset (overlap).BlockMap[f,list,{n1,n2,etc}]→ a block at a time, with dimensions n1 by n2.
(* typical usage *) BlockMap[f, {1,2,3,4}, 2] (* {f[{1, 2}], f[{3, 4}]} *) (* HHHH------------------------------ *) (* unfit tail are ignored *) BlockMap[f, {1,2,3,4}, 3] (* {f[{1, 2, 3}]} *) (* unfit tail are ignored *) BlockMap[f, {1, 2, 3, 4, 5}, 2] (* {f[{1, 2}], f[{3, 4}]} *)
(* overlap examples *) BlockMap[f, {1, 2, 3, 4, 5, 6}, 2, 1] (* {f[{1, 2}], f[{2, 3}], f[{3, 4}], f[{4, 5}], f[{5, 6}]} *)
(* example on blocks of matrix *) Partition[Range[16],4] (* {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} *) BlockMap[f, Partition[Range[16],4] , {2, 2}] (* {{ f[{{1, 2}, {5, 6}}], f[{{3, 4}, {7, 8}}]}, {f[{{9, 10}, {13, 14}}], f[{{11, 12}, {15, 16}}]}} *) (* map f to 2 by 2 block , with overlap offset 1 *) BlockMap[f, Partition[Range[16],4] , {2, 2}, 1] (* {{ f[{{1, 2}, {5, 6}}], f[{{2, 3}, {6, 7}}], f[{{3, 4}, {7, 8}}]}, { f[{{5, 6}, {9, 10}}], f[{{6, 7}, {10, 11}}], f[{{7, 8}, {11, 12}}]}, { f[{{9, 10}, {13, 14}}], f[{{10, 11}, {14, 15}}], f[{{11, 12}, {15, 16}}]}} *)
SubsetMap (Map to a Subset in a List)
SubsetMap-
SubsetMap[f, list, indexList]→ map at indexListSubsetMap[f, list, positionList]→ map at a list of Position spec.
Change a subset of elements in a list by applying a function to them, return the whole list.
This is different from
MapAt, because here the function is aware all other items. e.g. the function can beSorton the subset.the f must take a list and return a list.
SubsetMap[ Function[x, x+1], {0,0,0,0,0}, {2,3}] (* {0, 1, 1, 0, 0} *) SubsetMap[Sort,{9, 8, 2, 5, 0, 4}, {2,3}] (* {9, 2, 8, 5, 0, 4} *) SubsetMap[Reverse,{1, 2, 3, 4, 5, 6, 7}, {1,5,6}] (* {6, 2, 3, 4, 5, 1, 7} *) (* Map at position {2,2} *) SubsetMap[ Function[x, x+1], {0,{0,0},0}, {{2,2}}] (* {0, {0, 1}, 0} *) (* HHHH------------------------------ *) (* Map at position {2,2} and {3} *) SubsetMap[ Function[x, x+1], {0,{0,0},0}, {{2,2},{3}}] (* {0, {0, 1}, 1} *)
Wolfram. List Operations, and Loop, Iteration, Recursion
- Wolfram: List Operations
- Wolfram: List. Create (Table)
- Wolfram: Create Flat List (Range)
- Wolfram: List. Get Parts
- Wolfram: List. Add Element
- Wolfram: List. Delete Element
- Wolfram: List. Change Element
- Wolfram: List. Check Exist
- Wolfram: List. Join, Union, Intersection, Difference
- Wolfram: List. Min, Max
- Wolfram: List. Filter
- Wolfram: List. Sort Reverse Ordering
- Wolfram: Flatten
- Wolfram: Riffle (Add at Every Nth)
- Wolfram: RotateLeft
- Wolfram: Padding
- Wolfram: List. Partition, Reshape, Split, Gather
- Wolfram: Transpose
- Wolfram: List. Same Items Counts, Tally, Group
- Wolfram: List. Combinatorics
- Wolfram: Iteration
- Wolfram: Map Function to List
- Wolfram: Scan (foreach)
- Wolfram: Recursion
- Wolfram: Fold (reduce)
- Wolfram: Loop