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>Actually python converts both in the same way: <code>str(object)</code> calls <code>object.__str__()</code> or <code>object.__repr__()</code> if the former does not exist. From that point it is the responsibility of <code>object</code> to provide its string representation.</p>

<p>Python lists and numpy arrays are different objects, designed and implemented by different people to serve different needs so it is to be expected that their <code>__str__</code> and <code>__repr__</code> methods do not behave the same.</p>

python arrays string list numpy