Hi <span name="Nathaniel Smith" class="gD">Nathaniel,<br><br>thanks for your reply, it works fine and suffice for my purpose.<br><br>cheers,<br><br>Chao<br></span><br><div class="gmail_quote">On Sat, Mar 16, 2013 at 5:49 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><p dir="ltr">On 16 Mar 2013 16:41, "Chao YUE" <<a href="mailto:chaoyuejoy@gmail.com" target="_blank">chaoyuejoy@gmail.com</a>> wrote:<br>

><br>
> Dear all,<br>
><br>
> Is there some way to index the numpy array by specifying arbitrary axis and arbitrary slice, while<br>
> not knowing the actual shape of the data?<br>
> For example, I have a 3-dim data, data.shape = (3,4,5)<br>
> Is there a way to retrieve data[:,0,:] by using something like np.retrieve_data(data,axis=2,slice=0),<br>
> by this way you don't have to know the actual shape of the array.<br>
> for for 4-dim data, np.retrieve_data(data,axis=2,slice=0) will actually be data[:,0,:,:]</p>
</div><p dir="ltr">I don't know of anything quite like that, but it's easy to fake it:</p>
<p dir="ltr">def retrieve_data(a, ax, idx):<br>
    full_idx = [slice(None)] * a.ndim<br>
    full_idx[ax] = idx<br>
    return a[tuple(full_idx)]</p>
<p dir="ltr">Or for the specific case where you do know the axis in advance, you just don't know how many trailing axes there are, use<br>
    a[:, :, 0, ...]<br>
and the ... will expand to represent the appropriate number of :'s.</p><span class="HOEnZb"><font color="#888888">
<p dir="ltr">-n</p>
</font></span><br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div>***********************************************************************************</div>
<div>Chao YUE<br>Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)<br>UMR 1572 CEA-CNRS-UVSQ<br>Batiment 712 - Pe 119<br>91191 GIF Sur YVETTE Cedex</div>
<div>Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16<br></div>

<div>************************************************************************************</div>