Wolfram: List Reshape (split, group, flatten, transpose)
Function related to restructuring of list.
Partition (Split into Groups Evenly)
Partition
-
Partition[ list, n ]
Partition[ list, n, d ]
- and more
group every n items into sublist, with overlap of d items. (default is 0)
Partition[ {1, 2, 3, 4, 5, 6}, 2 ] (* {{1, 2}, {3, 4}, {5, 6}} *) (* with overlap *) Partition[ {1, 2, 3, 4, 5, 6}, 2, 1 ] (* {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}} *)
🛑 WARNING: unfit tail items are dropped.
(* unfit tail items are dropped *) Partition[ {1, 2, 3, 4, 5, 6, 7}, 3 ] (* {{1, 2, 3}, {4, 5, 6}} *)
Split into Sublist When Neighbors Are Different
Split
-
Split[ list ]
Split[ list, testF ]
- split a list into sublists at places were neighboring items are different, using testF.
- testF takes 2 args and return
True
orfalse
.
Split[ {2, 2, 3, 3, 3, 5} ] (* {{2, 2}, {3, 3, 3}, {5}} *) Split[ {2, 2, 3, 3, 3, 5}, Function[{x,y}, EvenQ[ x ] === EvenQ[ y ]] ] (* {{2, 2}, {3, 3, 3, 5}} *)
SplitBy
-
SplitBy[ list, f ]
SplitBy[ list, {f1, f2, etc} ]
- Split a list into sublists at places were neighboring items are different. Items are considered different if function f on them are different.
- If given list of functions {f1, f2, etc}, use subsequent functions to break a tie.
💡 TIP: this is like
Split
, but more convenient to use for the predicate function to determine if two items are same, because the function just need to take a single argument.SplitBy[ {2, 2, 3, 3, 3, 5}, OddQ ] (* {{2, 2}, {3, 3, 3, 5}} *)
Transpose
Transpose
-
Transpose[list]
Transpose[list, m <-> n]
Transpose[list,{n1, n2, etc}]
- Transpose a matrix.
- Transposes levels m and n. (default
1
and2
) - when given a list {n1, n2, etc}, transposes so that the k(th) level in list is the n(th) level in the result. (the list is a permutation of 1 to k, where k is
ArrayDepth
)
Transpose[ {{a, b, c}, {1, 2, 3}} ] (* {{a, 1}, {b, 2}, {c, 3}} *) (* same as *) Transpose[ {{a, b, c}, {1, 2, 3}}, {2, 1} ] (* {{a, 1}, {b, 2}, {c, 3}} *) (* same as *) Transpose[ {{a, b, c}, {1, 2, 3}}, 1 <-> 2 ] (* {{a, 1}, {b, 2}, {c, 3}} *)
Flatten
Flatten
-
Flatten[ list]
Flatten[ list, n]
Flatten[ list, n, h ]
flatten a list, up to level n (default
Infinity
), and only those with Head h (defaultList
)Flatten[ {3, {9, {4}}, 5} ] (* {3, 9, 4, 5} *) Flatten[ {3, {9, {4}}, 5}, 1 ] (* {3, 9, {4}, 5} *) Flatten[ {3, {9, {4}}, 5}, 2 ] (* {3, 9, 4, 5} *) (* flatten only those with head f *) Flatten[ {3, f[9, {4}], g[5]}, Infinity, f ] (* {3, 9, {4}, g[5]} *)
Intersperse, Add at Every Nth
Riffle
-
Riffle[ list, x ]
Riffle[ list, x, n ]
Riffle[ list, {x1, x2, etc} ]
intersperse x into the list, at every nth (default 2), or use a list of values {x1, x2, etc}
Riffle[ {1, 2, 3}, x ] (* {1, x, 2, x, 3} *) Riffle[ {1, 2, 3}, x, 2 ] (* {1, x, 2, x, 3} *) Riffle[ {1, 2, 3, 4, 5, 6}, x, 3 ] (* {1, 2, x, 3, 4, x, 5, 6} *) Riffle[ {1, 2, 3, 4, 5, 6}, x, 3 ] (* {1, 2, x, 3, 4, x, 5, 6} *) Riffle[ {1, 2, 3}, {a, b, c} ] (* {1, a, 2, b, 3, c} *)
Padding
PadLeft
-
WolframLang List Operations
- Wolfram: List Operations
- Wolfram: Create List (Table)
- Wolfram: Create Flat List (Range)
- Wolfram: Get Parts of List
- Wolfram: Add Element to List
- Wolfram: Delete Element in List
- Wolfram: Change Element in List
- Wolfram: Check Item Exist in List
- Wolfram: Filter List
- Wolfram: Sort, Reverse, Order
- Wolfram: List Reshape (split, group, flatten, transpose)
- Wolfram: List. Count, Group, Similar Items
- Wolfram: List Combinatorics
- Wolfram: List Join, Union, Intersection, Difference