如何在 Python 中执行楼层划分
已发表: 2022-11-02在本教程中,您将学习如何在 Python 中执行地板除法。 您将使用 Python 的 // 运算符、Python 数学模块中的 floor 函数等 - 以及代码示例。
我们将从 Python 中算术运算符的概述开始,并了解下除法运算符 // 的工作原理。 然后,我们将学习如何使用其他等效方法,包括数学和运算符模块中的函数来执行地板除法。
让我们开始吧…
Python中的算术运算符
在 Python 中,您可以使用算术运算符对int
和float
数据类型的数字执行简单的算术运算。 这些运算符作用于操作数(数字)并返回运算结果。

下表总结了 Python 中的算术运算符及其工作原理:
操作员 | 句法 | 结果 |
加法 (+) | num1+num2 | 返回num1 和num2 之和 |
减法 (-) | num1-num2 | 返回num1 和num2 之间的差 |
乘法 (*) | num1*num2 | 返回num1 和num2 的乘积 |
求幂 (**) | num1**num2 | 返回num1 的num2 次方的结果; num1 num2 |
分配 (/) | num1/num2 | 返回num1 除以num2 的结果——包括小数部分 |
楼层划分 (/) | num1//num2 | 返回num1 除以num2 时的商 |
模数 (%) | num1 % num2 | 返回num1 除以num2 时的余数 |
让我们举几个使用这些算术运算符的例子。 您可以在 Python REPL 或 Geekflare 的在线 Python 编辑器中试用这些示例。
>>> num1 = 18 >>> num2 = 5 >>> num1 + num2 23 >>> num1 - num2 13 >>> num1 * num2 90 >>> num1 ** num2 1889568
在本例中, num1
为 18, num2
为 5。除法运算num1/num2
返回包含小数部分的结果。
数字 5 进入 18 三次,剩下的为 3。 因此,地板除法运算num1//num2
给出商 3,而模运算符给出余数——在这种情况下也是 3。
>>> num1/num2 3.6 >>> num1//num2 3 >>> num1 % num2 3
这应该让您了解除法、地板除法和模运算符的工作原理。 接下来,我们将详细了解楼层除法算子。
️ 在 Python 2 中,除法运算 (/) 将结果截断为最接近的整数——类似于 Python 3 中的底除法运算。本教程讨论了 Python 3.x 中底除法运算的工作原理。
使用 // 运算符的楼层划分

考虑一个带有除数和除数的除法运算。 在num1/num2
中, num1
是被除数, num2
是除数。 要执行num1
和num2
的地板除法,请使用num1//num2
。
底除法运算符(//) 返回除法运算的商(整数或浮点数),具体取决于操作数的数据类型。
地板除法运算符不能确保答案始终是整数。 如果被除数 ( num1
) 或除数 ( num2
) 是浮点数,则num1//num2
的结果是浮点数。 这里有一些例子。
>>> 18.0//5 3.0 >>> 10.0//4 2.0 >>> 15//4.0 3.0
如果您需要结果为整数,则需要使用int()
函数将其显式转换为整数:
>>> int(18.0//5) 3 >>> int(10.0//4) 2 >>> int(15//4.0) 3
引擎盖下发生了什么?
当您使用地板除法运算符 // 时,特殊方法(也称为 dunder 方法) __floordiv__()
被调用。 因此,您也可以对任何整数或浮点数使用__floordiv__()
方法,如下所示:
num1 = 18 num2 = 5 num1.__floordiv__(num2) # Output: 3
使用 operator.floordiv() 进行楼层划分

要在 Python 中执行地板除法,您还可以使用operator
模块中的floordiv()
函数。
Python 的 operator 模块包含可以执行所有算术运算的高效函数的定义。 因此,要执行地板除法,您还可以使用 operator 模块中的floordiv()
函数 - 而不是 // 运算符。
使用 operator 模块中的floordiv()
函数等效于使用地板除法运算符。
>>> import operator >>> operator.floordiv(18,5) # Output: 3 >>> operator.floordiv(12,5.0) # Output: 2.0
使用 math.floor() 进行楼层划分
地板功能如何工作?
在数学中,
floor()
函数将任何实数x
作为输入并返回一个整数(结果)。 该结果是小于或等于实数 x 的最大整数。
为了更好地理解这一点,让我们举几个例子,并在数轴上可视化这些数字。
示例 1 :考虑数字 2.3。 小于等于2.3的最大整数是2; 所以 floor(2.3) 将返回 2。

示例2:您也可以在处理负数时应用相同的定义。 考虑数字-1.7。 小于等于-1.7的最大整数是-2; 所以 floor(-1.7) 将返回 -2。


让我们使用数学模块中的floor()
函数验证上述结果。
>>> from math import floor >>> floor(2.3) 2 >>> floor(-1.7) -2
要执行楼层划分,您可以使用num1/num2
作为参数调用floor()
函数。 由于它将结果截断或向下舍入到最接近的整数,它相当于地板除法运算。
您可以从math
模块显式导入floor()
函数,如下所示:
from math import floor num1 = 18 num2 = 5 floor(num1/num2) # Output: 3
或者,您也可以只导入math
模块,然后使用math.floor()
访问floor()
函数。
import math num1 = 18 num2 = 5 math.floor(num1/num2) # Output: 3
与 operator 模块中的floordiv()
函数和地板除法运算符 // 不同,使用math.floor(num1/num2)
可确保结果为整数。 这种方法使代码可读并消除了类型转换步骤。
import math num1 = 18.0 num2 = 5 math.floor(num1/num2) # Output: 3
Python中的楼层划分示例

让我们用一个实际的例子来结束我们的讨论:二分搜索。
二进制搜索是一种高效的搜索算法,可让您在O(log n)时间内通过排序数组搜索目标元素,其中n是数组的大小。
该算法通过在每一步将搜索间隔分成两半来工作。 这取决于间隔的中点是否与目标匹配(搜索在找到匹配时结束!)或小于或大于目标。 由于数组的大小在每一步都减少了一半,因此中点并不总是计算为整数。
itemlist = [5,7,18,21,34,45] item = 7
考虑以下二分搜索算法的实现。 函数binary_search()
接受一个数字 ( item
) 和一个列表 ( itemlist
) 并在itemlist
中搜索该item
的出现。
- 如果找到该
item
,则该函数返回该item
出现的索引。 - 否则,它返回
None
。
def binary_search(item, itemlist): # get the list size listsize = len(itemlist) - 1 # start at the two ends of the list lowerIdx = 0 upperIdx = listsize while lowerIdx <= upperIdx: # calculate the middle point # use normal division instead of floor division midPt = (lowerIdx + upperIdx)/ 2 # if item is found, return the index if itemlist[midPt] == item: return midPt # otherwise get the next midpoint if item > itemlist[midPt]: lowerIdx = midPt + 1 else: upperIdx = midPt - 1 if lowerIdx > upperIdx: return None
这个实现在功能上是正确的,除了我们没有考虑到在搜索进行时midPt
不评估为整数。
binary_search(item,itemlist)
如果我们调用该函数,我们会遇到一个TypeError
,指出列表索引必须是整数或切片,而不是浮点数。
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-a5f12ebc3145> in <module> ----> 1 binary_search(item,itemlist) <ipython-input-2-524ef6900b1f> in binary_search(item, itemlist) 12 13 # if item is found, return the index ---> 14 if itemlist[midPt] == item: 15 return midPt 16 # otherwise get the next midpoint TypeError: list indices must be integers or slices, not float
我们修改函数定义以使用地板除法运算符:
def binary_search(item, itemlist): # get the list size listsize = len(itemlist) - 1 # start at the two ends of the list lowerIdx = 0 upperIdx = listsize while lowerIdx <= upperIdx: # calculate the middle point # use floor division midPt = (lowerIdx + upperIdx)// 2 # if item is found, return the index if itemlist[midPt] == item: return midPt # otherwise get the next midpoint if item > itemlist[midPt]: lowerIdx = midPt + 1 else: upperIdx = midPt - 1 if lowerIdx > upperIdx: return None
该函数返回找到项目 7 的索引,即索引 1。
binary_search(item,itemlist) # Output: 1
结论
我希望本教程能帮助您了解如何在 Python 中执行地板除法。 以下是您学到的不同方法的摘要:
- 在 Python 中, a 运算符 b以 a 和 b 为操作数执行运算符定义的运算,并返回运算结果。
- 您可以使用 Python 的地板除法运算符 //; a//b返回除法运算 a/b 的商。
- 或者,您可以使用 Python 的 operator 模块中定义的等效floordiv()函数,语法为: operator.floordiv(a,b)来获取 a//b 的结果。
- 以上所有方法都返回商,但数据类型可以是浮点数或整数,具体取决于 a 和 b 的值。 因此,您必须将返回值转换为整数。
- Python 数学模块中的floor()函数也可用于执行地板除法: math.floor(a,b)等价于a//b并返回一个整数。 如果您希望结果为整数,请考虑使用数学模块中的 floor 函数。
接下来,学习如何在 Python 中使用 defaultdict。