Python 蟒蛇3:如何取整到一个特定的数字?
我想四舍五入到下一个1、2或5的十进制值,如下面的代码示例所示Python 蟒蛇3:如何取整到一个特定的数字?,python,python-3.x,Python,Python 3.x,我想四舍五入到下一个1、2或5的十进制值,如下面的代码示例所示 if result > 0.1: if result > 0.2: if result > 0.5: if result > 1.0: if result > 2.0: if result
if result > 0.1:
if result > 0.2:
if result > 0.5:
if result > 1.0:
if result > 2.0:
if result > 5.0:
if result > 10.0:
if result > 20.0:
if result > 50.0:
rounded_result = 100.0
else:
rounded_result = 50.0
else:
rounded_result = 20.0
else:
rounded_result = 10.0
else:
rounded_result = 5.0
else:
rounded_result = 2.0
else:
rounded_result = 1.0
else:
rounded_result = 0.5
else:
rounded_result = 0.2
else:
rounded_result = 0.1
例如,对于介于0.1和0.2之间的值,四舍五入结果应为0.2,对于介于0.2和0.5之间的值,四舍五入结果应为0.5,依此类推
有更聪明的方法吗?也许是这样的函数 它期望
阈值
按升序排列
def round_threshold(value, thresholds):
for threshold in thresholds:
if value < threshold:
return threshold
return value
thresholds = [0, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0]
for test in (0.05, 0.15, 11.3, 74, 116):
print(test, round_threshold(test, thresholds))
像这样的函数,也许 它期望
阈值
按升序排列
def round_threshold(value, thresholds):
for threshold in thresholds:
if value < threshold:
return threshold
return value
thresholds = [0, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0]
for test in (0.05, 0.15, 11.3, 74, 116):
print(test, round_threshold(test, thresholds))
thresholds=[0,0.1,0.2,0.5,1.0,2.0,5.0,10.0,20.0,50.0,100.0]
对于i,枚举中的阈值(阈值):
如果valuethresholds[-1]:
打印(阈值[-1])
打破
阈值=[0,0.1,0.2,0.5,1.0,2.0,5.0,10.0,20.0,50.0,100.0]
对于i,枚举中的阈值(阈值):
如果valuethresholds[-1]:
打印(阈值[-1])
打破
这是矢量化的替代方案:
def custom_round(value, boundaries):
index = np.argmin(np.abs(boundaries - value)) + 1
if index < boundaries.shape[0]:
return boundaries[index]
else:
return value
import numpy as np
boundaries = np.array([0, 0.1, 0.2, 0.5,
1.0, 2.0, 5.0, 10.0,
20.0, 50.0, 100.0])
## test is from @AKX (see other answer)
for test in (0.05, 0.15, 11.3, 74, 116):
print(test, custom_round(test, boundaries))
def自定义_圆(值、边界):
索引=np.argmin(np.abs(边界-值))+1
如果索引<边界形状[0]:
返回边界[索引]
其他:
返回值
将numpy作为np导入
边界=np.数组([0,0.1,0.2,0.5,
1.0, 2.0, 5.0, 10.0,
20.0, 50.0, 100.0])
##测试来自@AKX(见其他答案)
对于(0.05,0.15,11.3,74,116)中的试验:
打印(测试、自定义(测试、边界))
这是矢量化的替代方案:
def custom_round(value, boundaries):
index = np.argmin(np.abs(boundaries - value)) + 1
if index < boundaries.shape[0]:
return boundaries[index]
else:
return value
import numpy as np
boundaries = np.array([0, 0.1, 0.2, 0.5,
1.0, 2.0, 5.0, 10.0,
20.0, 50.0, 100.0])
## test is from @AKX (see other answer)
for test in (0.05, 0.15, 11.3, 74, 116):
print(test, custom_round(test, boundaries))
def自定义_圆(值、边界):
索引=np.argmin(np.abs(边界-值))+1
如果索引<边界形状[0]:
返回边界[索引]
其他:
返回值
将numpy作为np导入
边界=np.数组([0,0.1,0.2,0.5,
1.0, 2.0, 5.0, 10.0,
20.0, 50.0, 100.0])
##测试来自@AKX(见其他答案)
对于(0.05,0.15,11.3,74,116)中的试验:
打印(测试、自定义(测试、边界))
如果值大于100,该怎么办?@MurrayW如果结果>50.0:四舍五入\u result=100.0
每个大于50的数字都四舍五入到100。那么101.0将“四舍五入”到100?10000000将被“四舍五入”为100?@MurrayW为了简单起见,假设大于100的值不会出现,因为该公式返回值结果
(我没有将其添加到本文中,因为这不是问题的一部分)如果值大于100,会发生什么情况?@MurrayW如果结果>50.0:四舍五入\u result=100.0
每个大于50的数字都四舍五入到100。那么101.0将“四舍五入”到100?10000000将被“四舍五入”为100?@MurrayW为了保持简单,假设大于100的值不会出现,因为公式返回值<代码>结果代码>(我没有将其添加到本文中,因为这不是问题的一部分)。如果值是<代码>的,因为假设阈值是有序的,您可能可以使用对分
模块来避免线性搜索(尽管考虑到threholds列表有多短可能并不重要)<代码>对分。对分(阈值,值)
将返回值的插入点,它是下一个阈值的索引,您可以返回该索引。如果它返回len(thresholds)
,那么您就从列表的末尾掉了下来。您可以显式地处理该条件,或者使用切片使其隐式(并且有点难以读取):(阈值[bisect.bisect(thresholds,v):]或[v])[0]
。或者,您可以将next
与过滤生成器一起使用:next((如果值<阈值,则阈值中的t表示t),value)
将返回与筛选器匹配的第一个生成值。。。或者默认值。假设阈值是有序的,那么它不应该是if值吗?您可能可以使用对分
模块来避免线性搜索(尽管考虑到threholds列表有多短,这可能并不重要)<代码>对分。对分(阈值,值)
将返回值的插入点,它是下一个阈值的索引,您可以返回该索引。如果它返回len(thresholds)
,那么您就从列表的末尾掉了下来。您可以显式地处理该条件,或者使用切片使其隐式(并且有点难以读取):(阈值[bisect.bisect(thresholds,v):]或[v])[0]
。或者,您可以将next
与过滤生成器一起使用:next((如果值<阈值,则阈值中的t表示t),value)
将返回与筛选器匹配的第一个生成值。。。或者默认值。