什么是在给定范围之间制作包含均匀间隔数字(而不仅仅是整数)的任意长度列表的pythonic方法?例如:在python中制作一定范围内的均匀间隔数字的列表
my_func(0,5,10) # (lower_bound , upper_bound , length)
# [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ]
注意Range()
函数只处理整数。而这个:
def my_func(low,up,leng):
list = []
step = (up - low)/float(leng)
for i in range(leng):
list.append(low)
low = low + step
return list
好像太复杂了。有任何想法吗?
有一些很好的解决方案:http://stackoverflow.com/questions/477486/python-decimal-range-step-value – Pill
请注意,由于浮点数的(必要的)不准确性,这只适用于某些序列。 – delnan