Slice Ary[1::2, :3] row slice =1::2 start =1 stop =4 step =2 | Sololearn: Learn to code for FREE!


Slice Ary[1::2, :3] row slice =1::2 start =1 stop =4 step =2

plz explain me how this setp stop and start values are taken and the meaning of this [1::2]??

11/17/2019 1:20:37 PM

Âaryahi Âashi

7 Answers

New Answer


Russ That syntax is valid for a 2+ dimension ndarray. Âaryahi Âashi 1::2 basically says, start at index 1, go untill the end, and skip every other term (in this case, rows, because the first dimension is rows). :3 means start at index 0 and go until, but not including, index 3. this traverses the column because it is dimension 2.


Aymane Boukrouh [Unavailable] No, it is valid Python, just as I said only on NumPy ndarrays. I have used it before.


Rora I didn't know it worked with numpy array, good point 👌


First up, Ary[1::2, :3] is meaningless. That syntax is not how list slicing works. A list slice can take up to three arguments, separated by colons (":"). The first number is the first index you want included in your slice. If it is left blank (if there is no number entered before the first colon), it will take the first index as the start of your slice. The second number is the index at which the slice will end. If this is left blank, it include all indexes after the first. The third number indicates the step and it defaults to 1. A step of 2 will mean that it will take every second element after the first. Some examples: a = [1,2,3,4,5,6,7] a[2:] = [3,4,5,6,7] #3 is at index 2 and as there is no "end", it includes all elements up to the end of the list. a[1:4] = [2,3,4] a[:4] = [1,2,3,4] a[1::3] = [2,5] a[4::-1] = [5,4,3,2,1] a[5:1:-2] = [6,4]


Rora again, it probably works in javascript, but just like Russ said, it is not a valid syntax in python


Rora Thanks for the clarification.