python - 在 Python 中,根据表达式和特定区间创建数组
问题描述
我需要在 python 中创建一个程序,在其中我询问用户他们想要的间隔数,以及数组的特定间隔 [a,b]。由此,我需要基于表达式 (ba)/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))
推荐阅读
- typescript - 如何测试 NestJs 响应拦截器
- python - Python - 在传递参数时限制线程数
- mongodb - Mongo Query 获取过去 7 天内每分钟查看次数最多的前 10 个产品
- javascript - 如何为 TypeScript 项目的不同部分配置不同的 ES 目标?
- amazon-web-services - AWS 应用程序负载均衡器目标组日志
- web - 使用 C# 向策略服务器提交 Web 证书请求
- mysql - 获取拥有超过 2 个文档但没有类型 2 的所有联系人
- c - 如何在 mate-screensaver 中显示和隐藏“板载”屏幕键盘?
- php - PHP ImageClassifier 是用图像训练的,当测试再次对其进行分类时,我得到了相反的结果?
- typescript - 打字稿中的意外类型“从不”