2016-11-08 46 views
0
def interval(a,b,n): 
    dx = float(b-a)/(n+1) 
    cnt = 1 
    points = [a] 
    xj = a 
    while cnt <= n+1: 
     xj += dx 
     points.append(xj) 
     cnt+=1 
    return points 
+0

为什么你需要它作为一个列表理解? – wwii

+0

,因为列表理解比循环操作简单和短。 – user59220

+1

是什么问题?请尝试。 – wwii

回答

0

更换你的函数如下给出: -

def interval(a,b,n): 
    dx = float(b-a)/(n+1) 
    points = [a] 
    xj = a 
    points.extend([xj + (i * dx) for i in xrange(1, n+2)]) 
    return points