Calculating the range of the exact number of values ​​in Python

I am creating a range between two numbers (float), and I would like this range to have an exact fixed length (no more, no less). rangeand arangework with steps. To put things in pseudo Python, this is what I would like to achieve:

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]

This is essentially equivalent to a function R seq, which can indicate a sequence of a given length. Is this possible in Python?

Thank.

+3
source share
5 answers

Use linspace () from NumPy .

>>> 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]

It should be the most effective and accurate.

+5
source

. 66472: frange(), float (Python) float, .

, , decimal.Decimal float ( ), Python().

+4
def my_function(start, end, length):
    len = length - 1
    incr = (end-start) / len
    r = [ start ]
    for i in range(len):
        r.append ( r[i] + incr )
    return r
+2

:

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]

, , , .

+1

, Python . ; round_setting = '.01'. , .

#!/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

From there, you can use the usual step and the last step to create your list.

+1
source

Source: https://habr.com/ru/post/1737510/


All Articles