def frange(start, stop, step=1.0):
''' "range()" like function which accept float type'''
i = start
while i < stop:
yield i
i += step
# Generate one element at a time.
# Preferred when you don't need all generated elements at the same time.
# This will save memory.
for i in frange(1.0, 2.0, 0.5):
print i # Use generated element.
# Generate all elements at once.
# Preferred when generated list ought to be small.
print list(frange(1.0, 10.0, 0.5))
def any_number_range(a,b,s=1):
""" Generate consecutive values list between two numbers with optional step (default=1)."""
if (a == b):
return a
else:
mx = max(a,b)
mn = min(a,b)
result = []
# inclusive upper limit. If not needed, delete '+1' in the line below
while(mn < mx + 1):
# if step is positive we go from min to max
if s > 0:
result.append(mn)
mn += s
# if step is negative we go from max to min
if s < 0:
result.append(mx)
mx += s
return result
def custom_range(*args):
s = slice(*args)
start, stop, step = s.start, s.stop, s.step
if 0 == step:
raise ValueError("range() arg 3 must not be zero")
i = start
while i < stop if step > 0 else i > stop:
yield i
i += step
>>> [x for x in custom_range(10, 3, -1)]
import numpy as np
def generate_floating_numbers_in_range(start: int, end: int, step: float):
"""
Generate a list of floating numbers within a specified range.
:param start: range start
:param end: range end
:param step: range step
:return:
"""
numbers = np.linspace(start, end,(end-start)*int(1/step)+1).tolist()
return [round(num, 2) for num in numbers]