How do you get both a page number and a position within the page?
divmod computes the floor quotient and its matching remainder together. For a positive page size the pair reconstructs the offset.
numpy.divmod in practice
With four items per page, zero-based item nine lands on page index two, slot one. Add one separately when presenting page numbers to readers.
import numpy as np
[a.tolist() for a in np.divmod([0,4,9],4)]Result [[0, 1, 2], [0, 0, 1]]
中文
如何同时得到分页编号和页内位置?
divmod 同时计算向下取整的商与相应余数。正页宽下,两者可以重建原偏移。
numpy.divmod 算例
每页四项,从零计数的编号 9 对应第 2 页、页内位置 1。面向读者显示从一计数的页码时需另加一。
import numpy as np
[a.tolist() for a in np.divmod([0,4,9],4)]结果 [[0, 1, 2], [0, 0, 1]]