numpy.divmod#
- numpy.divmod(x1, x2, [out1, out2, ]/, [out=(None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'divmod'>#
同时返回元素级的商和余数.
np.divmod(x, y)等价于(x // y, x % y),但速度更快,因为它避免了冗余的工作.它用于在 NumPy 数组上实现 Python 内置函数divmod.- 参数:
- x1array_like
被除数数组.
- x2array_like
除数数组. 如果
x1.shape != x2.shape,则它们必须可广播到公共形状(这将成为输出的形状).- outndarray, None, or tuple of ndarray and None, optional
结果存储到的位置.如果提供,它必须具有输入的广播到的形状. 如果未提供或为 None,则返回一个新分配的数组.一个元组(可能只能作为关键字参数)必须具有等于输出数量的长度.
- wherearray_like, optional
此条件在输入上进行广播.在条件为 True 的位置, out 数组将设置为 ufunc 结果.否则, out 数组将保留其原始值.请注意,如果通过默认值
out=None创建一个未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化.- \kwargs
对于其他仅限关键字的参数,请参阅 ufunc docs .
- 返回:
- out1ndarray
整除后的元素级商.如果 x1 和 x2 都是标量,则这是一个标量.
- out2ndarray
整除后的元素级余数.如果 x1 和 x2 都是标量,则这是一个标量.
参见
floor_divide等价于 Python 的
//运算符.remainder等价于 Python 的
%运算符.modf对于正数
x,等价于divmod(x, 1),但返回值被交换.
示例
>>> import numpy as np >>> np.divmod(np.arange(5), 3) (array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))
divmod函数可以用作 ndarray 上np.divmod的简写形式.>>> x = np.arange(5) >>> divmod(x, 3) (array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))