我们可以从python获得如下输出吗?
text1 -9.98442 -10. -0.01558
text2 -0. -0. 0.
text3 0. -0. -0.
text4 0.24829 0.24829 -0.
text5 -7.40799 -7.40575 0.00224
text6 0. -0. -0.
text7 -0. 0. 0.
text8 -5.88917 -5.83199 0.05718
text9 -0. 0. 0.
text10 -6.83455 -6.74592 0.08862
左侧是文本,周期对齐列,抑制小数(例如,从一维字符串数组和二维浮点数组)。< / p>
Numpy可以使用np.set_printoptions(precision=5,suppress=True)
和print(array)
生成浮点数的结构,但后来我不知道如何将文本放到左边,最好摆脱括号。
答案 0 :(得分:0)
花了比预期更多的时间我有一个程序化的解决方案,我选择完全压制零。
script.py:
import numpy as np
def printskip(text,mat,tol=5,off=6):
rows = np.size(mat,0)
cols = np.size(mat,1)
lim = 10**(-tol)
zero = ' '*off+'-'+' '*tol
form = ':>'+str(off+tol+1)+'.'+str(tol)+'f}'
for row in range(rows):
textform=''
for i in range(cols):
if abs(mat[row,i]) < lim:
txtf = zero
else:
txtf = '{'+str(i)+form
textform += txtf
print('{:<10}'.format(text[row]),textform.format(*mat[row,:]))
text = ['dkl', 'dofj', 'ldfj','gasf']
dat = np.array([
[0.2621206 , 0.1006 , 0.620606],
[200.0000005 , 200.3832 , 0.062532e-7],
[10.4001095 , 0.2393 , 0.009593],
[0.0373096 , 1.e-7 , 1000.809681]
])
printskip(text,dat,5)
python3 script.py
dkl 0.26212 0.10060 0.62061
dofj 200.00000 200.38320 -
ldfj 10.40011 0.23930 0.00959
gasf 0.03731 - 1000.80968