[Numpy-discussion] Numpy arrays and slicing comprehension issue
Jaime Fernández del Río
jaime.frio at gmail.com
Sat Jul 8 07:03:58 EDT 2017
The last index is exclusive:
[a:b] means a <= index < b.
On Jul 8, 2017 10:20 AM, <paul.carrico at free.fr> wrote:
> Hi
>
> Once again I need your help to understand one topic concerning slicing
> topic, or in other word I do not understand how it works in that particular
> (but common) case; I’m trying to reassign the 4 first values in an array:
>
> - If I use [:3] I’m expecting to have 4 values (index 0 to 3 included)
> - Ditto with [0:3]
> - If I use [3:] I have 2 values as expected (indexes 3 and 4)
>
> Both code and results are presented here after, so this way of thinking
> worked so far in other calculations, and it fails here?
>
>
> Thanks
>
> Paul
>
> ps : extraction from the doc (https://docs.scipy.org/doc/
> numpy/reference/arrays.indexing.html)
>
> *[... all indices are zero-based ...]*
>
>
>
> *Code*:
>
> x = np.random.rand(5); print("x = ",x);
>
> ## test 1
>
> print("partials =\n %s \nor %s \nor %s" %( x[:3], x[0:3], x[3:]) )
>
> print("x[0] : ",x[0]); print("x[1] : ",x[1]); print("x[2] : ",x[2]);
> print("x[3] : ",x[3])
>
>
>
> ## test 2
>
> y = np.ones(4); print("y = ",y)
>
> x[0:4] = y
>
> print("x final = ",x)
>
>
> *Provide*:
>
> x = [ 0.39921271 0.07097531 0.37044695 0.28078163 0.11590451]
>
> partials =
>
> [ 0.39921271 0.07097531 0.37044695]
>
> or [ 0.39921271 0.07097531 0.37044695]
>
> or [ 0.28078163 0.11590451]
>
> x[0] : 0.39921271184
>
> x[1] : 0.0709753133926
>
> x[2] : 0.370446946245
>
> x[3] : 0.280781629
>
> y = [ 1. 1. 1. 1.]
>
> x final = [ 1. 1. 1. 1. 0.11590451]
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170708/c8e55e21/attachment.html>
More information about the NumPy-Discussion
mailing list