在Python中,根据表达式和特定区间创建数组

In Python, create an array based on an expression and specific interval

我需要在 python 中创建一个程序,在其中询问用户他们想要的间隔数,以及数组的特定间隔 [a,b]。由此,我需要根据表达式 (b-a)/n.

创建数组的元素

到目前为止,我的代码如下所示:


n= int(input("Enter the number of intervals: "))
a= float(input("Enter the min point of the interval: "))
b= float(input("Enter the max point of the interval: "))

xPoints=list() #create a list of the x points
xPoints.append(float(a))

#code for elements in between a and b

xPoints.append(float(b))
print(xPoints)

任何有关阵列的帮助将不胜感激。谢谢!

def get_array(n, a, b):
    if n == 0:
        return []
    elif n == 1:
        return [a, b]
    else:
        return [a] + [a+i*((b-a)/n) for i in range(1, n)] + [b]

n = int(input("Enter the number of intervals: "))
a = float(input("Enter the lowest number of the interval: "))
b = float(input("Enter the highest number of the interval: "))

print(get_array(n, a, b))