0
Guys please help,some1 explain to me the concept of using the (:) after the number -of row and columns -and before it.
3 ответов
+ 2
Hi! lesson 34.1 list slices (python course for beginners)
+ 2
Kawtar Chicha
I modified your code so that the numbers are not confusing:
import numpy as np
A = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(A[:2, 2:])
result is [[3]
[6]]
You can visualize the indexing as:
[
[0, 1, 2], --> 0
[0, 1, 2], --> 1
[0, 1, 2] --> 2
]
The first slice :2 selects the indices
going downward from index 0 but not including index 2
[0, 1, 2], --> 0
[0, 1, 2], --> 1
or
[1, 2, 3],
[4, 5, 6],
The second slice 2: selects the indices going to the left starting from index 2
[ 2],
[ 2],
or
[ 3]
[ 6]
+ 1
I do not know much about numpy but you are slicing the array/list when using ':'.Take a look at this: https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-list-slicing/