Reputation: 61
What I need is to divide/spread 0 to 1. according to single number which is more than 2. like number 5 so 0 to 5 will be divided like this
0.00
0.25
0.50
0.75
1.00
5 values in a list
and my other question is what to do to get a sequence like this where middle number is 1 and first and last number is 0 , if number is 10.
0.00
0.25
0.50
0.75
1.00
1.00
0.75
0.50
0.25
0.00
Upvotes: 3
Views: 572
Reputation: 476659
The upper bound of the range(..)
is exclusive (meaning it is not enumerated), so you need to add one step to the range(..)
function:
for i in range(0,11):
b = i*(1.0/10)
print b
That being said, if you want to create such array, you can use numpy.arange(..)
:
>>> import numpy as np
>>> np.arange(0, 1.1, 0.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
This thus allows you to specify floats for the offset, end, and step parameter.
As for your second question, you can itertools.chain
iterables together, like:
from itertools import chain
for i in chain(range(0, 11), range(10, -1, -1)):
print(i/10.0)
Here we thus have one range(..)
that iterates from 0
to 10
(both inclusive), and one that iterates from 10
, to 0
(both inclusive).
Upvotes: 5
Reputation: 7303
range 0 to 10 will give you numbers from 0 to 9. Here is some practical to explain:
>>> list(range(0,10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(0,11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> list(range(0,1))
[0]
>>>
Upvotes: 2