How do you recover variable-length sequences from split points?

split cuts an array at index boundaries, each of which starts the next segment.

numpy.split in practice

Records of lengths two, three and one have internal cut points at two and five. Including the total length as another cut creates an empty final segment.

import numpy as np
[part.tolist() for part in np.split(np.arange(6), [2,5])]

Result [[0, 1], [2, 3, 4], [5]]

中文

如何按已知边界拆开拼接的序列?

split 可以按索引边界切开数组。每个边界指示下一段的起点。

numpy.split 算例

长度为 2、3、1 的三条记录依次存储,切点是累计长度 2 和 5。最后一个切点等于总长度时会多出空段。

import numpy as np
[part.tolist() for part in np.split(np.arange(6), [2,5])]

结果 [[0, 1], [2, 3, 4], [5]]

References / 参考来源

Keep reading / 继续阅读