# [Numpy-discussion] Numpy arrays and slicing comprehension issue

paul.carrico at free.fr paul.carrico at free.fr
Sat Jul 8 04:20:06 EDT 2017

``` 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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170708/ae5a54d1/attachment.html>
```