Finding Max Shape of Multiple .npz files
<p>I have a number of .npz files that potentially vary in shape and I'd like to find which file has the larges shape. The npzs have 2 arrays within in them, and I'm looking for the largest of the 2nd.  The following snippet works, but it takes longer than I expected to return shapes.  Is this the most efficient way of achieving this?   I'm worried about scaling because it currently takes a couple seconds to find the max shape[1] and I'm only looping through 4 arrays</p>

<pre><code>frameMax =0 
for f in npzs:
    d = np.load(f,mmap_mode='r')
    if d['arr_0'].shape[1]&gt;frameMax:
        frameMax = d['arr_0'].shape[1]
    d=None
</code></pre>

<p>Bear in mind that I/O operations might be relatively slow. That said, you can reduce the logic for finding the maximum to the following using the builtin <a href="https://docs.python.org/2/library/functions.html#max" rel="nofollow"><code>max</code></a> which will run in <a href="http://stackoverflow.com/questions/5454030/how-efficient-is-pythons-max-function">O(n)</a> time, and removes the need for the assignments you did:</p>

<pre><code>frameMax = max([np.load(f,mmap_mode='r')['arr_0'].shape[1] for f in npzs])
</code></pre>

python numpy