Okay, I have to admit this is kind of cheating, but, since Mathematica hasn't had an ad hoc function for the frequently-used list equi-division... we can write a shorthand by ourselves?
ClearAll[Backslash];
Backslash[
ls_?ListQ /; (D`len = Length[ls]; True),
n_?IntegerQ /; 1 <= n <= D`len && (D`n = n; True),
Optional[nth_?IntegerQ /; 1 <= nth <= D`n, All]
] := If[D`m = D`len~Quotient~n; nth === All,
Partition[ls, D`m][[;; n]],
ls[[(nth - 1) D`m + 1 ;; nth*D`m]]
]
Then you can ignore it and type like Esc
\
Esc
4
to split a list into 4 equi-length sublists (leaving out the leftover elements):
Range[10]\4 (* \ = \[Backslash] *)
{{1, 2}, {3, 4}, {5, 6}, {7, 8}}
Take the first half of a list:
Range[10]\2\1
{1, 2, 3, 4, 5}
They don't need to be of strictly equal size? Not a problem. According to this, we can make modifications:
ClearAll[Backslash];
Backslash[
ls_?ListQ /; (D`len = Length[ls]; True),
n_?IntegerQ /; 1 <= n <= D`len && (D`n = n; True),
Optional[nth_?IntegerQ /; 1 <= nth <= D`n, All]
] := If[nth === All,
ls~TakeList~Table[Quotient[D`len + k, n], {k, 0, n - 1}],
ls[[(D`s = Sum[Quotient[D`len + k, n], {k, 0, nth - 2}]) + 1
;; D`s + Quotient[D`len + nth - 1, n]]]
]
Example:
Range[10]\4
{{1, 2}, {3, 4}, {5, 6, 7}, {8, 9, 10}}
Range[10]\4\3
{5, 6, 7}
The query part is short, although... Alright, just take this as humor.
Partition
- this is easy to find in the documentation - I don't know how you missed it - if you want the irregular elements usingUpTo
inPartition[{a, b, c, d, e}, UpTo[3]]
gives {{a,b,c},{d,e}} $\endgroup$Partition[Array[a, 7], 3]
, i.e. leaving out superflous elements. I will edit my question to make a clear difference (and leave it up to the Stackexchange gods to see whether it is different enough). $\endgroup$RatioPartition
seems to be another possibility:ResourceFunction["RatioPartition"][Range[21], {50,50}]
$\endgroup$