可以使用 python 中的 numpy 库来格式化输出数组。举个例子:
import numpy asnp
a = np.array([1, 2, 3])
np.set_printoptions(precision=3) # 设置精度
print(a)
运行上述代码会输出:[1. 2. 3.]
使用np.set_printoptions()
函数来设置输出的精度。
如果需要更多的格式化选项,可以参考numpy官方文档 https://numpy.org/doc/stable/reference/generated/numpy.set_print