python - 如何打印一组带有格式的向量?
问题描述
我一直在尝试以类似表格的格式打印 2 个向量。当列表 A 和列表 B 中的元素没有相同数量的浮点数时,问题就来了
import math
def f(x):
y=1/(math.sqrt(1+x))
return y
xmin=0.0
xmax=1.0
print "Write some n:"
n=input()
x=[]
delta=((xmax-xmin)/n)
for i in range (n):
xx=xmin+i*delta
x.append(xx)
y=[]
b=f(x[0])
y.append(b)
for i in range (1,n-1):
yy=2*f(x[i])
y.append(yy)
y.append(f(x[n-1]))
for i in range(n):
print x[i],y[i]
打印最后一行时出现问题:我想要什么:
0.0 1.0
0.125 1.88561808316
0.25 1.788854382
我得到什么:
0.0 1.0
0.125 1.88561808316
0.25 1.788854382
如何修复这些数字,以便程序正确打印结果?谢谢 :)
解决方案
使用字符串格式来做到这一点。以下会将填充设置为 6 个位置:
print(”{:6}{:6}".format(x[i ], y[I]))
推荐阅读
- php - PHP - 如何通过对数组中的相同元素进行分组来创建多维数组?
- sql - 为什么不过滤?SQL查询“在哪里”
- javascript - addEventListener('DOMContentLoaded') 重载问题
- xcode - Xcode:是否可以导出 SF 符号并导入 Xcode 以支持旧版本?
- image - gatsby : 优化没有'childImageSharp' graphql 字段的图像
- c# - 首先在 Entity Framework 6.4.4 数据库中实现自定义导航属性
- c++ - 使用 SDL2 渲染器绘制填充四边形/三角形的最快方法?
- recursion - 当我在 max 变量下方运行查询时,没有更新,我不明白为什么
- c# - 保存 16 位灰度图像的结构
- linux - phpspreadsheets/phpoffice 在 linux 上不工作