在Python中计算精确数量的值的范围
我正在两个数字(浮动)之间建立一个范围,我希望这个范围是一个精确的固定长度(不多也不少)<代码>范围和在Python中计算精确数量的值的范围,python,list,range,Python,List,Range,我正在两个数字(浮动)之间建立一个范围,我希望这个范围是一个精确的固定长度(不多也不少)范围和范围使用步骤。要将内容放入伪Python,我希望实现以下目标: start_value = -7.5 end_value = 0.1 my_range = my_range_function(star_value, end_value, length=6) print my_range [-7.50,-5.98,-4.46,-2.94,-1.42,0.10] 这本质上等同于R函数seq,它可以指定给
范围
使用步骤。要将内容放入伪Python,我希望实现以下目标:
start_value = -7.5
end_value = 0.1
my_range = my_range_function(star_value, end_value, length=6)
print my_range
[-7.50,-5.98,-4.46,-2.94,-1.42,0.10]
这本质上等同于R函数seq
,它可以指定给定长度的序列。这在Python中可能吗
谢谢。请参阅各种浮点实现的优缺点
或者,如果精度对您很重要,请使用decimal.decimal
而不是中回答的float(转换为然后返回)。如何:
def my_range_function(start, end, length):
if length <= 1: return [ start ]
step = (end - start) / (length - 1)
return [(start + i * step) for i in xrange(length)]
当然,它充满了舍入错误,但这正是使用浮点运算时得到的结果。从中使用linspace()
它应该是最有效和最准确的。为了处理舍入错误,下面的代码使用Python的。您可以设置舍入;对于这个示例,我通过
round\u setting='.01'
将其设置为两个小数点。为了处理任何舍入错误,将最后一步调整为余数
代码
输出
在此基础上,您可以使用正常步骤和最后一个步骤创建列表。使用浮动并不能使精确计算步骤变得更容易。范围与这些值完全一致是否重要,步长是否完全相同,或者舍入误差是否正常?我使用浮点,因为这些值来自另一个计算。但是,范围的端点可以是整数(例如0)。这些值必须精确,否则我会得到其他类型的错误。
numpy.arange
就是这样做的。问题是,这些函数使用步骤,而我需要事先确定确切的长度。这真的会阻止您在使用该函数之前:inc=(end-start)/length
,还是让另一个函数这样做并调用真正的实现?不过,如果您想要精确性,您应该尝试使用十进制,然后再转换回浮点。调用tolist()
成员函数,而不是使用需要迭代器的list
函数,list(linspace(…)
?我知道在某个地方会有一个函数来做这件事!!
def my_range_function(start, end, length):
if length <= 1: return [ start ]
step = (end - start) / (length - 1)
return [(start + i * step) for i in xrange(length)]
[-7.5, -5.9800000000000004, -4.46,
-2.9399999999999995, -1.4199999999999999, 0.099999999999999645]
>>> from numpy import linspace
>>> linspace(-7.5, 0.1, 6)
array([-7.5 , -5.98, -4.46, -2.94, -1.42, 0.1])
>>> linspace(-7.5, 0.1, 6).tolist()
[-7.5, -5.9800000000000004, -4.46, -2.9399999999999995, -1.4199999999999999, 0.10000000000000001]
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
import math
import decimal
start_value = -7.5
end_value = 0.1
num_of_steps = 6
def my_range(start_value, end_value, num_of_steps):
round_setting = '.01'
start_decimal = decimal.Decimal(str(start_value)).quantize(
decimal.Decimal(round_setting))
end_decimal = decimal.Decimal(str(end_value)).quantize(
decimal.Decimal(round_setting))
num_of_steps_decimal = decimal.Decimal(str(num_of_steps)).quantize(
decimal.Decimal(round_setting))
step_decimal = ((end_decimal - start_decimal) /
num_of_steps_decimal).quantize(decimal.Decimal(round_setting))
# Change the last step in case there are rounding errors
last_step_decimal = (end_decimal - ((num_of_steps - 1) * step_decimal) -
start_decimal).quantize(decimal.Decimal(round_setting))
print('Start value = ', start_decimal)
print('End value = ', end_decimal)
print('Number of steps = ', num_of_steps)
print('Normal step for range = ', step_decimal)
print('Last step used for range = ', last_step_decimal)
my_range(start_value, end_value, num_of_steps)
$ ./fixed_range.py
Start value = -7.50
End value = 0.10
Number of steps = 6
Normal step for range = 1.27
Last step used for range = 1.25