string(array) vs string(list) in python
<p>I was constructing a database for a deep learning algorithm. The points I'm interested in are these:</p>

<pre><code>with open(fname, 'a+') as f:
    f.write("intens: " + str(mean_intensity_this_object) + "," + "\n")
    f.write("distances: " + str(dists_this_object) + "," + "\n")
</code></pre>

<p>Where <code>mean_intensity_this_object</code> is a list and <code>dists_this_object</code> is a <code>numpy.array</code>, something I didn't pay enough attention to to begin with. After I opened the file, I found out that the second variable, <code>distances</code>, looks very different to <code>intens</code>: The former is </p>

<pre><code>distances: [430.17802963  315.2197058   380.33997833  387.46190951   41.93648858
  221.5210474   488.99452579],
</code></pre>

<p>and the latter </p>

<pre><code>intens: [0.15381262,..., 0.13638344],
</code></pre>

<p>The important bit is that the latter is a standard list, while the former is very hard to read: multiple lines without delimiters and unclear rules for starting a new line. Essentially as a result I had to rerun the whole tracking algorithm and change <code>str(dists_this_object)</code> to <code>str(dists_this_object.tolist())</code> which increased the file size. </p>

<p>So, my question is: why does this happen? Is it possible to save <code>np.array</code> objects in a more readable format, like lists? </p>

<p>In an interactive Python session:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; x = np.arange(10)/.33    # make an array of floats
&gt;&gt;&gt; x
array([  0.        ,   3.03030303,   6.06060606,   9.09090909,
        12.12121212,  15.15151515,  18.18181818,  21.21212121,
        24.24242424,  27.27272727])
&gt;&gt;&gt; print(x)
[  0.           3.03030303   6.06060606   9.09090909  12.12121212
  15.15151515  18.18181818  21.21212121  24.24242424  27.27272727]
&gt;&gt;&gt; print(x.tolist())
[0.0, 3.0303030303030303, 6.0606060606060606, 9.09090909090909, 12.121212121212121, 15.15151515151515, 18.18181818181818, 21.21212121212121, 24.242424242424242, 27.27272727272727]
</code></pre>

<p>The standard display for a list is with <code>[]</code> and <code>,</code>.  The display for an array is without <code>,</code>.  If there are over 1000 items, the array display employs an ellipsis</p>

<pre><code>&gt;&gt;&gt; print(x)
[    0.             3.03030303     6.06060606 ...,  3024.24242424
  3027.27272727  3030.3030303 ]
</code></pre>

<p>while the list display continues to show every value.</p>

<p>In this line, did you add the <code>...</code>, or is that part of the print? </p>

<pre><code>intens: [0.15381262,..., 0.13638344],
</code></pre>

<p>Or doing the same with a file write:</p>

<pre><code>In [299]: with open('test.txt', 'w') as f:
     ...:     f.write('array:'+str(x)+'\n')
     ...:     f.write('list:'+str(x.tolist())+'\n')

In [300]: cat test.txt
array:[  0.           3.33333333   6.66666667  10.          13.33333333
  16.66666667  20.          23.33333333  26.66666667  30.        ]
list:[0.0, 3.3333333333333335, 6.666666666666667, 10.0, 13.333333333333334, 16.666666666666668, 20.0, 23.333333333333336, 26.666666666666668, 30.0]
</code></pre>

<p><code>np.savetxt</code> gives more control over the formatting of an array, for example:</p>

<pre><code>In [312]: np.savetxt('test.txt',[x], fmt='%10.6f',delimiter=',')
In [313]: cat test.txt
  0.000000,  3.333333,  6.666667, 10.000000, 13.333333, 16.666667, 20.000000, 23.333333, 26.666667, 30.000000
</code></pre>

<p>The default array print is aimed mainly at interactive work, where you want to see enough of the values to see whether they are right or not, but you don't intend to reload them.  The <code>savetxt/loadtxt</code> pair are better for that.</p>

<p>The <code>savetxt</code> does, roughly:</p>

<pre><code>for row in x:
   f.write(fmt%tuple(row))
</code></pre>

<p>where <code>fmt</code> is constructed from your input paramater and the number of items in the <code>row</code>, e.g.   <code>', '.join(['%10.6f']*10)+'\n'</code></p>

<pre><code>In [320]: print('[%s]'%', '.join(['%10.6f']*10)%tuple(x))
[  0.000000,   3.333333,   6.666667,  10.000000,  13.333333,  16.666667,  20.000000,  23.333333,  26.666667,  30.000000]
</code></pre>

python arrays string list numpy