How to efficiently output n elements per string from a numpy array
I have a 1-D numpy array which is quite long. I would like to efficiently write it to a file by putting N spaces on each line in the file. I've tried several methods, but both have big problems.
First, I tried resizing the array by N columns. Given a file descriptor, f:
myArray.reshape(-1, N)
for row in myArray:
print >> f, " ".join(str(val) for val in row)
This was quite efficient, but requires the array to be a multiple of N elements. If the last line only contains one element (and N is more than one), I would only like to print 1 element ... not a crash.
Then I tried printing the counter and inserting a line break after every Nth item:
i = 1
for val in myArray:
if i < N:
print >> f, str(val)+" ",
i+=1
else:
print >> f, str(val)
i = 1
This worked great for any array of length, but was very slow (taking at least 10x more than my first option). I have been dumping many files from many arrays and cannot use this method due to speed.
Any thoughts on an efficient way to accomplish this inference?
for i in range(0, len(myArray), N):
print " ".join([str(v) for v in myArray[i:i+N]])
# or this
# print " ".join(map(str, myArray[i:i+N].tolist()))
You can add try
/ except
to your transform approach to print the last elements in the output file:
myArray.reshape(-1, N)
try:
for row in myArray:
print >> f, " ".join(str(val) for val in row)
except: # add the exact type of error here to be on the save side
# just print the last (incomplete) row
print >> f, " ".join(str(val) for val in myArray[-1])