在python中使用List comprehension而不是loop

时间:2016-11-08 06:59:01

标签: python-2.7 list-comprehension

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

1 个答案:

答案 0 :(得分: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