[Numpy-discussion] use slicing as argument values?
Jonathan Helmus
jjhelmus at gmail.com
Thu Jul 12 16:55:00 EDT 2012
On 07/12/2012 04:46 PM, Chao YUE wrote:
> Hi Ben,
>
> it helps a lot. I am nearly finishing a function in a way I think
> pythonic.
> Just one more question, I have:
>
> In [24]: b=np.arange(1,11)
>
> In [25]: b
> Out[25]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>
> In [26]: b[slice(1)]
> Out[26]: array([1])
>
> In [27]: b[slice(4)]
> Out[27]: array([1, 2, 3, 4])
>
> In [28]: b[slice(None,4)]
> Out[28]: array([1, 2, 3, 4])
>
> so slice(4) is actually slice(None,4), how can I exactly want retrieve
> a[4] using slice object?
>
> thanks again!
>
> Chao
slice is a build in python function and the online docs explain its use
(http://docs.python.org/library/functions.html#slice). b[slice(4,5)]
will give you something close to b[4], but not quite the same.
In [8]: b[4]
Out[8]: 5
In [9]: b[slice(4,5)]
Out[9]: array([5])
- Jonathan Helmus
More information about the NumPy-Discussion
mailing list