索引和选择 (Indexing and Selection)
# importing module
import numpy as np
# array declaration
arr = np.arange(0,11)
# printing array
print(arr)
Output
输出量
[ 0 1 2 3 4 5 6 7 8 9 10]
Now, to get the index of an element in a Numpy array, it is very similar to how the index is retrieved from a list, for example (Note: In python, the index starts from 0).
现在,要获取Numpy数组中元素的索引,例如,它与从列表中检索索引的方式非常相似( 注意:在python中,索引从0开始)。
# importing module
import numpy as np
# array declaration
arr = np.arange(0,11)
# printing array
print("arr:", arr)
# printing elements based on index
# returns element from given index
print("arr[8]:", arr[8])
# returns the elements between start, end
print("arr[1:5]:", arr[1:5])
# returns the elements between start, end
print("arr[0:5]:", arr[0:5])
#returns the elements until end
print("arr[:6]:", arr[:6])
# returns the elements from start
print("arr[2:]:", arr[2:])
Output
输出量
arr: [ 0 1 2 3 4 5 6 7 8 9 10]
arr[8]: 8
arr[1:5]: [1 2 3 4]
arr[0:5]: [0 1 2 3 4]
arr[:6]: [0 1 2 3 4 5]
arr[2:]: [ 2 3 4 5 6 7 8 9 10]
索引二维数组 (Indexing a 2-D array)
Consider the following example,
考虑以下示例,
# importing module
import numpy as np
# 2-D array
arr_2