import numpy as np
arr1 = np.arange(2,14)
print(arr1)
out:
[ 2 3 4 5 6 7 8 9 10 11 12 13]
print(arr1[4])
out:6
print(arr1[1:4])#不包含第4个位置,左闭右开
out:
[3 4 5]
print(arr1[5:-1])#最后一位是-1,不包含最后一位
out:
[ 7 8 9 10 11 12]
print(arr1[-2:])#最后两个
out:
[12 13]
arr2 = arr1.reshape(3,4)
print(arr2)
out:
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
print(arr2[2])
out:
[10 11 12 13]
print(arr2[1][2])
out:8
print(arr2[:,1])#所有行中的第二列
out:[ 3 7 11]
for i in arr2:#迭代行
print(i)
out:
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]
print(arr2)
out:
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
for i in arr2.T:#转置,迭代列
print(i)
out:
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
for i in arr2.flat:#扁平化,一个一个迭代
print(i)
out:
2
3
4
5
6
7
8
9
10
11
12
13
numpy基础--5.numpy的索引
最新推荐文章于 2024-11-13 17:24:18 发布