Confusing selector?
I am really new to numpy but I just found that v[2:4] means selecting v[2] and v[3]. V[4] is not included! This is quite different from the conventions of matlab and R. _______________________________________________________=0A= =0A= The information in this email or in any file attached=0A= hereto is intended only for the personal and confiden-=0A= tial use of the individual or entity to which it is=0A= addressed and may contain information that is propri-=0A= etary and confidential. If you are not the intended=0A= recipient of this message you are hereby notified that=0A= any review, dissemination, distribution or copying of=0A= this message is strictly prohibited. This communica-=0A= tion is for information purposes only and should not=0A= be regarded as an offer to sell or as a solicitation=0A= of an offer to buy any financial product. Email trans-=0A= mission cannot be guaranteed to be secure or error-=0A= free. P6070214=0A=
On 2/15/07, Geoffrey Zhu <gzhu@peak6.com> wrote:
I am really new to numpy but I just found that v[2:4] means selecting v[2] and v[3]. V[4] is not included! This is quite different from the conventions of matlab and R.
Yes, it is the Python slicing convention and rather similar to for loops in C where for(i = 0; i < N; i++) is a common construction. It is an consequence of zero based indexing. Matlab started in Fortran and uses the conventions of the Fortran do loop, which no doubt makes sense for arrays whose base index is 1. Slicing in NumPy, and now Python, also includes a step, so you can write a[0:n:2] to reference all the elements with even indices. Chuck
participants (2)
-
Charles R Harris
-
Geoffrey Zhu