one iteration of k-means

def _recenter(self):
        """
        one iteration of k-means
        """
        for split_idx in range(len(self._splits)):
            split = self._splits[split_idx]
            len_idx = self._split2len_idx[split]
            if split == self._splits[-1]:
                continue
            right_split = self._splits[split_idx + 1]

            # Try shifting the centroid to the left
            if len_idx > 0 and self._lengths[len_idx - 1] not in self._split_cntr:
                new_split = self._lengths[len_idx - 1]
                left_delta = self._len_cntr[split] * (right_split - new_split) - self._split_cntr[split] * (
                        split - new_split)
                if left_delta < 0:
                    self._splits[split_idx] = new_split
                    self._split2len_idx[new_split] = len_idx - 1
                    del self._split2len_idx[split]
                    self._split_cntr[split] -= self._len_cntr[split]
                    self._split_cntr[right_split] += self._len_cntr[split]
                    self._split_cntr[new_split] = self._split_cntr[split]
                    del self._split_cntr[split]

            # Try shifting the centroid to the right
            elif len_idx < len(self._lengths) - 2 and self._lengths[len_idx + 1] not in self._split_cntr:
                new_split = self._lengths[len_idx + 1]
                right_delta = self._split_cntr[split] * (new_split - split) - self._len_cntr[split] * (
                        new_split - split)
                if right_delta <= 0:
                    self._splits[split_idx] = new_split
                    self._split2len_idx[new_split] = len_idx + 1
                    del self._split2len_idx[split]
                    self._split_cntr[split] += self._len_cntr[split]
                    self._split_cntr[right_split] -= self._len_cntr[split]
                    self._split_cntr[new_split] = self._split_cntr[split]
                    del self._split_cntr[split]