<p dir="auto">As brought up at the mailinglist, this code ates up much memory:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="spectrumArray = np.random.randint(0,255, (2**20,2**12)).astype(np.uint8)"><pre class="notranslate"><code class="notranslate">spectrumArray = np.random.randint(0,255, (2**20,2**12)).astype(np.uint8)
</code></pre></div>
<blockquote>
<p dir="auto">It has also crossed my mind that np.random.randint and np.random.rand could use an extra 'dtype' keyword. It didn't look easy to implement though.</p>
</blockquote>
<p dir="auto">See this thread on the mailinglist: <a href="https://mail.scipy.org/pipermail/numpy-discussion/2015-December/074306.html" rel="nofollow">[Numpy-discussion] array of random numbers fails to construct</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; dtype = numpy.dtype([('position', numpy.int32, 3)])
&gt;&gt;&gt; values = [([1, 2, 3],), ([4, 5, 6],)]
&gt;&gt;&gt; a = numpy.array(values, dtype=dtype)
&gt;&gt;&gt; a.tolist()
[(array([1, 2, 3], dtype=int32),), (array([4, 5, 6], dtype=int32),)]"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; dtype = numpy.dtype([('position', numpy.int32, 3)])
&gt;&gt;&gt; values = [([1, 2, 3],), ([4, 5, 6],)]
&gt;&gt;&gt; a = numpy.array(values, dtype=dtype)
&gt;&gt;&gt; a.tolist()
[(array([1, 2, 3], dtype=int32),), (array([4, 5, 6], dtype=int32),)]
</code></pre></div>
<p dir="auto">In the code snippet above, I would have thought that <code class="notranslate">a.tolist()</code> would return <code class="notranslate">[([1, 2, 3],), ([4, 5, 6],)]</code> instead of a structure still containing non-expanded versions of <code class="notranslate">ndarray</code> objects.</p>
<p dir="auto">Is this behaviour to be expected?</p>
<p dir="auto">I wrote a quick and dirty workaround but this is far from optimal as it duplicates part of the work of <code class="notranslate">ndarray.tolist</code>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def array_to_list(array):
    if isinstance(array, numpy.ndarray):
        return array_to_list(array.tolist())
    elif isinstance(array, list):
        return [array_to_list(item) for item in array]
    elif isinstance(array, tuple):
        return tuple(array_to_list(item) for item in array)
    else:
        return array"><pre class="notranslate"><code class="notranslate">def array_to_list(array):
    if isinstance(array, numpy.ndarray):
        return array_to_list(array.tolist())
    elif isinstance(array, list):
        return [array_to_list(item) for item in array]
    elif isinstance(array, tuple):
        return tuple(array_to_list(item) for item in array)
    else:
        return array
</code></pre></div>
<p dir="auto">For reference, this question was first raised on StackOverflow <a href="http://stackoverflow.com/questions/39502461/truly-recursive-tolist-for-numpy-structured-arrays" rel="nofollow">http://stackoverflow.com/questions/39502461/truly-recursive-tolist-for-numpy-structured-arrays</a></p>