如何优化表并实现循环

时间:2016-04-03 10:14:43

标签: python arrays multidimensional-array

我的想法是,我是一名刚从某些实验中获取数据的学生,我需要在表格中表示。我使用数组来存储用户输入的所有数据,但我正在寻找一种更有效的方式来表示我的数据。

这是我的代码:

import numpy as np
print 'Times, Average Times, Velocity'
tteb=np.zeros((3,7))
pos=np.array([1000,950,850,700,500,250,0])
posp=pos*(5.16667*10**(-4))
for j in range (0,3):
    k=j+1
    print 'This is for trial %d' %k
    for i in range (0,7):
        print 'Input for band %d'%i     
        tteb[j,i]=float(raw_input('~'))
print 'Trials 1-3 for all 7 bands.:'
print tteb
raw_input('Press [Enter] to continue to average time and *velocity *(later).')

ttebatvsum=tteb.sum(axis=0)
print 'This is all the times added together. (Bands 0--->6).'
print ttebatvsum
print 'This is the average for all of the times. (Bands 0--->6).'
ttebatvmean=ttebatvsum/3
print ttebatvmean
raw_input('Press [Enter] to continue to velocity.')
velocity=posp/ttebatvsum
print 'Here are all the velocities. (Bands 0--->6).'
print velocity

#Table Starts here
print 'Pos (ml)   |Pos (m)    |  t1       |t2         |  t3        |t(avg)      |v           |'
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[0],posp[0],tteb[0,0],tteb[1,0],tteb[2,0],ttebatvmean[0],velocity[0])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[1],posp[1],tteb[0,1],tteb[1,1],tteb[2,1],ttebatvmean[1],velocity[1])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[2],posp[2],tteb[0,2],tteb[1,2],tteb[2,2],ttebatvmean[2],velocity[2])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[3],posp[3],tteb[0,3],tteb[1,3],tteb[2,3],ttebatvmean[3],velocity[3])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[4],posp[4],tteb[0,4],tteb[1,4],tteb[2,4],ttebatvmean[4],velocity[4])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[5],posp[5],tteb[0,5],tteb[1,5],tteb[2,5],ttebatvmean[5],velocity[5])
print '%2.3f      |%4.3f      |%6.3f      |%8.3f      |%10.3f      |%12.3f      |%14.3f      |'%(pos[6],posp[6],tteb[0,6],tteb[1,6],tteb[2,6],ttebatvmean[6],velocity[6])

我的想法是在我的情况下使用for循环。我想让数组的数量增加1

2 个答案:

答案 0 :(得分:0)

看一下这些行:

print ...(pos[0],posp[0],tteb[0,0],tteb[1,0],tteb[2,0],ttebatvmean[0],velocity[0])
print ...(pos[1],posp[1],tteb[0,1],tteb[1,1],tteb[2,1],ttebatvmean[1],velocity[1])

如果我们有一个从零开始的变量j,我们可以写:

print ...(pos[j],posp[j],tteb[0,j],tteb[1,j],tteb[2,j],ttebatvmean[j],velocity[j])

所以我们可以把它放在一个循环中:

for j in range(0,7):
    print ...(pos[j],posp[j],tteb[0,j],tteb[1,j],tteb[2,j],ttebatvmean[j],velocity[j])

但请注意you can print without a newline (carriage return),因此您无需在一行中执行tteb全部操作。事实上,你可以这样做:

for i in range(0,num_trials):
    sys.stdout.write('|%8.3f      '%(tteb[i,j]))

将其放入:

for j in range(0,7):
    sys.stdout.write('%2.3f      |%4.3f'%     (pos[j],posp[j])
    for i in range(0,num_trials):
        sys.stdout.write('|%8.3f      '%(tteb[i,j]))
    sys.stdout.write('|%12.3f      |%14.3f      |'%(ttebatvmean[j],velocity[j])
    sys.stdout.write('\n')  #newline has to be printed manually for stdout

希望您可以看到我们可以将写入分解为更易于管理的块,并在需要时在一行内使用循环。

NB :我不确定你在那里使用的打印格式是什么,格式应该是'%8.3f',意思是8个字符的总数,3位数字精确的固定精度格式。除非您想要在小数位前面加上更多或更少的数字,否则8不应该改变。

答案 1 :(得分:0)

另一种方法是将数据整形为numpy中的表,然后输出整个数据。您还可以使用format更好地控制字符串的显示方式。

# Put all the output data into one 2d array
table = np.concatenate((np.stack((pos, posp)), tteb, np.stack((ttebatvmean, velocity))))

# Rotate and flip the table so it's oriented the right way for output
table = np.flipud(np.rot90(table))

# Set output format
heading = "{:>8s}|" + "{:>8s}|" * 6
row = "{:>8.0f}|" + "{:>8.3f}|" * 6

# Print headings and data
print(heading.format("Pos (ml)", "Pos (m)", "t1", "t2", "t3", "t(avg)", "v"))
for data_row in table:
    print(row.format(*data_row))

如果您不想将所有数据放入单个数组中,您还可以将数组zip放在一起进行输出:

tteb_rotated = np.flipud(np.rot90(tteb))
for p, pp, (t1, t2, t3), tavg, v in zip(pos, posp, tteb_rotated, ttebatvmean, velocity):
    print(row.format(p, pp, t1, t2, t3, tavg, v))