Slice Ary[1::2, :3] row slice =1::2 start =1 stop =4 step =2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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]??

17th Nov 2019, 1:20 PM
Âaryahi Âashi
Âaryahi Âashi - avatar
7 Answers
+ 3
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.
17th Nov 2019, 3:33 PM
Rora
+ 3
Aymane Boukrouh [Unavailable] No, it is valid Python, just as I said only on NumPy ndarrays. I have used it before.
17th Nov 2019, 3:46 PM
Rora
+ 2
Rora I didn't know it worked with numpy array, good point 👌
17th Nov 2019, 3:51 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
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]
17th Nov 2019, 2:20 PM
Russ
Russ - avatar
0
Rora again, it probably works in javascript, but just like Russ said, it is not a valid syntax in python
17th Nov 2019, 3:45 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Rora Thanks for the clarification.
17th Nov 2019, 5:15 PM
Russ
Russ - avatar
17th Nov 2019, 5:28 PM
Abdul Wahab Chattha
Abdul Wahab Chattha - avatar