1

What I am doing: generating a series of long 1D arrays.

What I want to do: append/concatentate/vstack/? these into a 2D array, then save the rows as columns in a csv file.

The following works, but it's not elegant:

rlist=[]                        # create empty list
for i in range(nnn)             # nnn is typically 2000
    (calculate an array "r")
    rlist.append(r)             # append f.p. array to rlist
rarr = array(rlist)             # turn it back into array so I can transpose
numpy.savetxt('test.csv',rarr.T,delimiter=',')  # save rows as columns in csv file

Is there a more elegant or pythonesque way to do it?

dcnicholls
  • 391
  • 1
  • 5
  • 15
  • Why do you say this is not elegant? Sure, you could use `numpy.vstack` and a transpose -- But in the end, it will be the same number of lines of code and you'll either be growing the same array at every iteration of the loop (which is likely to be inefficient) or waiting until the end to stack them all together (which is what you're doing now). I don't see much difference ... – mgilson Aug 02 '12 at 12:24

2 Answers2

1

If you know the length of r and nnn in advance, you can do:

rarr = np.zeros((r_len, nnn)) # r_len rows, nnn columns
for i in range(nnn):
    rarr[:,i] = r_calc()
numpy.savetxt('test.csv', rarr, delimiter=',')

This puts the data vectors directly into rows of rarr, saving you the conversion to array and transpose.

mtrw
  • 34,200
  • 7
  • 63
  • 71
  • Preallocating the array is a good approach. I'm not sure it is any more elegant, but it is likely to be a bit faster. – senderle Aug 02 '12 at 13:18
0
rarr = np.concatenate([get_array(r) for r in xrange(nnn)])
np.savetxt('test.cvs', rarr.T, delimiter=',')
Nicolas Barbey
  • 6,639
  • 4
  • 28
  • 34
  • Thanks for those suggestions. I'm a real Python newbie, so I expect my code to be ugly. Maybe not as bad as I thought! I'll work through the ideas. Excellent way to learn Python, but (coming from FORTRAN) Python's methods aren't alway obvious. – dcnicholls Aug 02 '12 at 14:03