I have an numpy array of form
a = [1,2,3]
which I want to save to a .txt file such that the file looks like:
1 2 3
If I use numpy.savetxt then I get a file like:
1
2
3
There should be a easy solution to this I suppose, any suggestions?
Very very easy: [1,2,3]
A list is like a column.
If you want a list like a row, double corchete:
and
Finally:
Note, the comma between square brackets, inner list are elements of the outer list
I found that the first solution in the accepted answer to be problematic for cases where the newline character is still required. The easiest solution to the problem was doing this:
I know this is old, but none of these answers solved the root problem of numpy not saving the array row-wise. I found that this one liner did the trick for me:
If
numpy >= 1.5
, you can do:Edit
several 1D arrays with same length
several 1D arrays with variable length
An alternative answer is to reshape the array so that it has dimensions
(1, N)
like so: