Reputation: 13
I'm making a Quantile problems and I need to do something like this
Intervals:
150-155 155-160 160-165 165-170 170-175 175-180 180-185
>> inferior_limit = 150
>> superior_limit = 185
>> range = inferior_limit - superior_limit
>> number_of_intervals = 5
Those are the variables and I need that because I'm doing a table's interval
>> width = range/number_of_intervals
>> while inferior_limit <= superior_limit
# there is my problem
>> inferior_limit += width
>> print inferior_limit
Upvotes: 1
Views: 634
Reputation: 133574
>>> start, stop, step = 150, 185, 5
>>> r = range(start, stop + 1, step) # You can use xrange on py 2 for greater efficiency
>>> for x, y in zip(r, r[1:]):
print '{0}-{1}'.format(x, y)
150-155
155-160
160-165
165-170
170-175
175-180
180-185
A more efficient way of doing this is through the use of the itertools
pairwise recipe.
from itertools import tee, izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
for x, y in pairwise(r):
print '{0}-{1}'.format(x, y)
Also just for fun here is a solution using itertools.starmap
, since nobody ever uses it!
from itertools import starmap
print '\n'.join(starmap('{0}-{1}'.format, pairwise(r)))
Upvotes: 1
Reputation: 362786
Is this what you meant?
>>> inf, sup, delta = 150, 185, 5
>>> print '\n'.join('{}-{}'.format(x, x + delta) for x in xrange(inf, sup, delta))
150-155
155-160
160-165
165-170
170-175
175-180
180-185
Upvotes: 2