Numpy 的 divmod(~)
方法為輸入數組中的每個被除數 x
和除數 y
返回元組 (x//y, x%y)
。這裏,//
表示整數除法,而 %
表示標準 Python 模數。
如果您需要這兩個數量,請使用 divmod(~)
方法,因為它可以防止冗餘計算。
參數
1. x1
| array_like
股息。
2. x2
| array_like
除數。
3. out
| Numpy array
| optional
您可以將計算的平均值放入 out
指定的數組中,而不是創建新數組。
4. where
| boolean
的array
| optional
標記為 False 的值將被忽略,即它們的原始值將未被初始化。如果指定了 out 參數,行為會略有不同 - 原始值將保持不變。
返回值
輸入數組中每個被除數 x
和除數 y
的元組 (x//y, x%y)
例子
公約數
x = [3, 8.5, -7]
np.divmod(x, 3)
(array([ 1., 2., -3.]), array([0. , 2.5, 2. ]))
為了澄清輸出是如何計算的:
3 // 3 = 1
8.5 // 3 = 2
-7 // 3 = -3
3 % 3 = 0
8.5 % 3 = 2.5
-7 % 3 = 2
多個除數
x = [3, 8.5, -7]
np.divmod(x, [1,2,3])
(array([ 3., 4., -3.]), array([0. , 0.5, 2. ]))
相關用法
- Python divmod()用法及代碼示例
- Python divmod方法用法及代碼示例
- Python NumPy divide方法用法及代碼示例
- Python distributed.protocol.serialize.register_generic用法及代碼示例
- Python distributed.get_task_metadata用法及代碼示例
- Python distributed.Client.gather用法及代碼示例
- Python distributed.recreate_tasks.ReplayTaskClient.recreate_task_locally用法及代碼示例
- Python distributed.diagnostics.plugin.SchedulerPlugin用法及代碼示例
- Python distributed.Client.ncores用法及代碼示例
- Python distributed.Client.retire_workers用法及代碼示例
- Python distributed.Client.unregister_worker_plugin用法及代碼示例
- Python distributed.fire_and_forget用法及代碼示例
- Python dir用法及代碼示例
- Python distributed.Client.set_metadata用法及代碼示例
- Python dictionary cmp()用法及代碼示例
- Python distributed.Client.scheduler_info用法及代碼示例
- Python distributed.Client.submit用法及代碼示例
- Python distributed.Client.compute用法及代碼示例
- Python distributed.SpecCluster.scale用法及代碼示例
- Python distributed.get_worker用法及代碼示例
- Python distributed.SpecCluster.scale_up用法及代碼示例
- Python difflib.unified_diff用法及代碼示例
- Python distributed.Client.nthreads用法及代碼示例
- Python distributed.comm.resolve_address用法及代碼示例
- Python distributed.Client.unpublish_dataset用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | divmod method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。