Wolfram: List. Partition, Reshape, Split, Gather
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}} *)
ArrayReshape
ArrayReshape[ {1, 2, 3, 4, 5, 6}, {2,3} ] (* {{1, 2, 3}, {4, 5, 6}} *) ArrayReshape[ {1, 2, 3, 4, 5, 6}, {3,2} ] (* {{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
Trueorfalse.
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}} *)
Group a List into Sublists Same Items
Gather-
Gather[ list ]Gather[ list, testF ]
- group same items together into a sublist. (reorder if necessary.)
- optionally by a function testF.
- testF takes 2 args and return
Trueorfalse.
Gather[ {5, 5, 8, 6, 5, 4} ] (* {{5, 5, 5}, {8}, {6}, {4}} *) Gather[ {5, 5, 8, 6, 5, 4}, Function[{x, y}, EvenQ[ x ] === EvenQ[ y ]] ] (* {{5, 5, 5}, {8, 6, 4}} *) GatherBy-
GatherBy[ list, f ]GatherBy[ list, {f1, f2, etc} ]
- group same items together into sublists, reorder if necessary, by comparing the result of function f to each item.
- f takes 1 arg and can return anything.
- If given list of functions {f1, f2, etc}, use subsequent functions to refine.
(* group same items into sublists *) GatherBy[ {5, 5, 8, 6, 5, 4}, Identity ] (* {{5, 5, 5}, {8}, {6}, {4}} *) (* group even and odd *) GatherBy[ {5, 5, 8, 6, 5, 4}, EvenQ ] (* {{5, 5, 5}, {8, 6, 4}} *) (* group integers and non integers *) GatherBy[ {5, 5.2, 8.6, 5, 4}, IntegerQ ] (* {{5, 5, 4}, {5.2, 8.6}} *) (* group by length *) GatherBy[ {5, 5, {8, 3}, 5, {4, 2}}, Length ] (* {{5, 5, 5}, {{8, 3}, {4, 2}}} *) (* group by checking is factor of 3 *) GatherBy[ {30, 752, 333, 298, 903}, Function[{x}, Mod[ x, 3 ] == 0 ] ] (* {{30, 333, 903}, {752, 298}} *)
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