Mathematica has two very useful functions to group an array into a list of smaller arrays based on given criteria: Split[] and SplitBy[] which I need to emulate in Python3 code:
Split[list,test] treats pairs of adjacent elements as identical whenever applying the function "test" to them yields True,
SplitBy[list,f] splits list into sublists consisting of runs of successive elements that give the same value when f is applied.
Thus if
a=[2,3,5,7,11,13,17,19,23,29]
Split[a,(#2-#1 < 4)&] gives:
[[2,3,5,7],[11,13],[17,19],[23],[29]]
and SplitBy[a,(Mod[#,2]==0)&] gives:
[[2],[3,5,7,11,13,17,19,23,29]]
In practice the array to be split might be a 2-dimensional table and the test functions might work on the elements in individual columns.
How can this behaviour coded efficiently in Python3?