Python sequence of decimal numbers
Rather than using a decimal step directly, it's much safer to express this in terms of how many points you want. Otherwise, floating-point rounding error is likely to give you a wrong result. Show Use the
If you really want to use a
floating-point step value, use
Floating-point rounding error will cause problems, though. Here's a simple case where rounding error causes
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Jan 25, 2009 at 12:26
Andrew JaffeAndrew Jaffe 25.8k4 gold badges49 silver badges59 bronze badges 8
Use a list comprehension instead to obtain a list of steps:
More generally, a generator comprehension minimizes memory allocations:
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Jan 25, 2009 at 10:35 8 Building on
'xrange([start], stop[, step])', you can define a generator that accepts and produces any type you choose (stick to types supporting
answered Jan 25, 2009 at 11:57
gimelgimel 80.2k10 gold badges74 silver badges104 bronze badges 6 Increase the magnitude of
EDIT: I honestly cannot remember why I thought that would work syntactically
That should have the desired output. answered Jan 25, 2009 at 10:32
cmsjrcmsjr 54.4k10 gold badges70 silver badges62 bronze badges 3 NumPy is a bit overkill, I think.
Generally speaking, to do a step-by-
( answered Feb 28, 2017 at 2:06
KalleKalle 13.1k7 gold badges61 silver badges76 bronze badges 1
answered May 17, 2014 at 20:50
1 Similar to R's
Results
answered Jun 11, 2012 at 19:10
zeferinozeferino 5001 gold badge5 silver badges11 bronze badges 4 The range() built-in function returns a sequence of integer values, I'm afraid, so you can't use it to do a decimal step. I'd say just use a while loop:
If you're curious, Python is converting your 0.1 to 0, which is why it's telling you the argument can't be zero. answered Jan 25, 2009 at 10:32
DanaDana 30.9k17 gold badges62 silver badges72 bronze badges 1 Here's a solution using itertools:
Usage Example:
answered Nov 8, 2012 at 10:00
PramodPramod 5,0303 gold badges43 silver badges43 bronze badges 1
in Python 2.7x gives you the result of:
but if you use:
gives you the desired:
Liam McInroy 4,2945 gold badges31 silver badges52 bronze badges answered May 13, 2012 at 23:20
NikNik 1511 silver badge3 bronze badges 0
Steve Czetty 6,0979 gold badges38 silver badges48 bronze badges answered Sep 7, 2012 at 2:01
RajaRaja 1171 silver badge2 bronze badges 1 And if you do this often, you might want to save the generated list
answered Jan 25, 2009 at 10:48
RSabetRSabet 5,9583 gold badges26 silver badges26 bronze badges
Output
This tool also works for answered Feb 9, 2018 at 0:31
pylangpylang 36.4k11 gold badges120 silver badges110 bronze badges Best Solution: no rounding error
Or, for a set range instead of set data points (e.g. continuous function), use:
To implement a function: replace
tripleee 164k27 gold badges244 silver badges296 bronze badges answered Dec 10, 2016 at 19:03
JasonJason 1,82922 silver badges18 bronze badges My versions use the original range function to create multiplicative indices for the shift. This allows same syntax to the original range function. I have made two versions, one using float, and one using Decimal, because I found that in some cases I wanted to avoid the roundoff drift introduced by the floating point arithmetic. It is consistent with empty set results as in range/xrange. Passing only a single numeric value to either function will return the standard range output to the integer ceiling value of the input parameter (so if you gave it 5.5, it would return range(6).) Edit: the code below is now available as package on pypi: Franges
answered Dec 12, 2012 at 22:29
Nisan.HNisan.H 5,8022 gold badges22 silver badges25 bronze badges 2 Lots of the solutions here still had floating point errors in Python 3.6 and didnt do exactly what I personally needed. Function below takes integers or floats, doesnt require imports and doesnt return floating point errors.
answered Feb 5, 2019 at 18:49
kowpowkowpow 851 silver badge8 bronze badges 3 Suprised no-one has yet mentioned the recommended solution in the Python 3 docs:
Once defined, the recipe is easy to use and does not require
I quote a modified version of the full Python 3 recipe from Andrew Barnert below:
answered Mar 2, 2018 at 15:00
Chris_RandsChris_Rands 36.4k12 gold badges79 silver badges110 bronze badges This is my solution to get ranges with float steps.
The output is:
answered Dec 12, 2013 at 17:04
Carlos VegaCarlos Vega 1,3312 gold badges13 silver badges35 bronze badges 3 For completeness of boutique, a functional solution:
answered Sep 12, 2015 at 7:44
Bijou TrouvailleBijou Trouvaille 8,1264 gold badges38 silver badges42 bronze badges You can use this function:
answered Jun 25, 2010 at 17:57
3 It can be done using Numpy library. arange() function allows steps in float. But, it returns a numpy array which can be converted to list using tolist() for our convenience.
answered Feb 12, 2016 at 12:18
start and stop are inclusive rather than one or the other (usually stop is excluded) and without imports, and using generators
answered Mar 1, 2018 at 21:53
Goran B.Goran B. 5344 silver badges13 bronze badges I know I'm late to the party here, but here's a trivial generator solution that's working in 3.6:
then you can call it just like the original answered Jan 2, 2019 at 21:07
1 To counter the float precision issues, you could use the This demands an extra effort of converting to
Sample outputs -
answered Mar 4, 2018 at 13:05
shad0w_wa1k3rshad0w_wa1k3r 12.6k8 gold badges65 silver badges88 bronze badges 5 Add auto-correction for the possibility of an incorrect sign on step:
answered Dec 1, 2010 at 6:36
My solution:
answered Jul 25, 2012 at 13:50
Here is my solution which works fine with float_range(-1, 0, 0.01) and works without floating point representation errors. It is not very fast, but works fine:
answered Jan 8, 2013 at 21:21
pymenpymen 4,89739 silver badges33 bronze badges I am only a beginner, but I had the same problem, when simulating some calculations. Here is how I attempted to work this out, which seems to be working with decimal steps. I am also quite lazy and so I found it hard to write my own range function. Basically what I did is changed my So I decided to test if my solution will work for my range by running a short test:
And it printed True for each. Now, if I'm getting it totally wrong, please let me know. answered Oct 10, 2013 at 22:27
The trick to avoid round-off problem is to use a separate number to move through the range, that starts and half the step ahead of start.
Alternatively, answered Sep 2, 2015 at 7:28
wolfram77wolfram77 2,5513 gold badges20 silver badges28 bronze badges My answer is similar to others using map(), without need of NumPy, and without using lambda (though you could). To get a list of float values from 0.0 to t_max in steps of dt:
answered Feb 16, 2017 at 19:48
How do you make a list of decimals in Python?This will give you the list you want: [Decimal('-2.0') + Decimal(n) / Decimal(10) for n in range(0,41)] . I get better results when giving strings to Decimal , e.g. np. arange(Decimal('-2.0'), Decimal('2.0'), Decimal('0.1')) .
How do you do range with decimals?(x * 0.1 for x in range(0, 10)) . Even better, put x/10 instead of x * 0.1 :D. Nothing special actually, but some numbers in there will be more precise, e.g. for 3*0.1 you get 0.30000000000000004 , while for 3/10 you get 0.3 :) 3/10 gives me 0, not 0.3.
How do you increment decimals in Python?Use np. linspace() to build an incrementing list of decimal values. Call np. linspace(start, stop, num) to return an evenly spaced list of numbers from start to stop that is num enteries long.
How do you enter a decimal range in Python?Use NumPy's arange() and linspace() functions to use decimal numbers in a start, stop and step argument to produce a range of floating-point numbers. Use Python generator to produce a range of float numbers without using any library or module.
|