def _groupby_leading_idxs(shape):
 idxs = itertools.product(*[range(s) for s in shape])
 return itertools.groupby(idxs, (lambda x: x[:(-1)]))

def _groupby_leading_idxs ( shape ) : idxs = itertools . product ( * [ range ( s ) for s in shape ] ) return itertools . groupby ( idxs , ( lambda x : x [ : ( - 1 ) ] ) )
group the indices for shape by the leading indices of shape .