0

The idea is that I'm a student that is just taking some data for some experiment and I need to represent it in a table. I used an array to store all the data that the users enter but I am looking for a more efficient way of representing my data.

Here is my code:

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])

The idea is to use for loops in my case. I want to make the array numbers go up by increments of 1

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • BTW- Using python 2.7.11, with Anaconda2 –  Apr 03 '16 at 10:19
  • There are modules for displaying numpy data nicely, without having to write individual `print` statements. See http://stackoverflow.com/questions/9712085/numpy-pretty-print-tabular-data – Stuart Apr 03 '16 at 12:01
  • @Stuart , Thanks however, for this coursework I am not allowed to venture onto other modules apart from numpy, math, pylab and a few other stuff –  Apr 03 '16 at 12:05

2 Answers2

0

Look at the set of lines:

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])

If we had a variable j, which started at zero, we could write:

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

So we can put that in a loop:

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

However, note that you can print without a newline (carriage return), so you don't have to do the tteb all on one line. In fact, you can do this:

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

Putting that in:

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

Hopefully you can see that we can break the writing down into more manageable chunks, and use loops within a line if needed.

NB: I'm not sure what you're doing with the print formats there, the format should be something like '%8.3f' meaning 8 total characters of number, with 3 digits of precision in fixed precision format. The 8 shouldn't change unless you want more or fewer digits in front of the decimal place.

Community
  • 1
  • 1
Phil H
  • 19,928
  • 7
  • 68
  • 105
0

An alternative way of doing this is to shape the data into a table in numpy and then output the whole thing. You can also use format to get better control over how the strings appear.

# 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))

If you don't want to put all the data into a single array you could also zip the arrays together for output:

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))
Stuart
  • 9,597
  • 1
  • 21
  • 30