0
Float list in Python
How to create a range list with float numbers: numbers = list(range(0, 10, 0.5))
3 Answers
+ 9
# using numpy:
import numpy as np
l = list(np.arange(0, 10, .5))
# manually:
l = [n/2 for n in range(20)]
+ 3
There is no built-in function to create a range of floats. So you have to write a function to do this task for you.
You can try this:
new_lst = []
for i in range(0, 11):
new_lst.append(float(i))
new_lst.append(float(i + 0.5))
print(new_lst)
+ 2
An other version, more flexible than the first one.
# creating range of floats. It does currently not work for negativ numbers.
start = 0.0
stop = 100.0
step = 0.253
res_lst = []
res_buf = start
while res_buf <= stop:
res_lst.append(format(round(res_buf, 3),'.3f'))
res_buf += step
print(res_lst)
https://code.sololearn.com/cbAe1UaBucxD/?ref=app