import numpy as np
#基础索引
x1 = np.arange(10)
x2 = np.arange(20).reshape(4,5)
print(x1)
print('-----')
print(x1[0],x1[9],x1[-1])
print('-----')
print(x1[2:4])
print('-----')
print(x1[2:-1])
print('-----')
print(x1[-3:])
print('-----')
print(x1[:-3])
print('-----')
print(x1[:3])
print('****************************************')
print(x2)
print('-----')
print(x2[0,0])
print('-----')
print(x2[-1,2])
print('-----')
print(x2[-1])
print('-----')
print(x2[:-1])
print('-----')
print(x2[:2,2:4])
print('-----')
print(x2[:,-1])
print('-----')
print(x2[:,:-1])
print('****************************************')
arr = np.random.randint(1,100,10)#随机生成1到100的10个数字
print(arr)
#arr.argsort()会返回排序后的索引index
#取最大值对应的3个下标
print(arr.argsort()[-3:])
print(arr[arr.argsort()[-3:]])
print('****************************************')
#布尔索引
x1 = np.arange(10)
print(x1>5)
print('-----')
print(x1[x1>5])
#eg:
x1[x1<=5]=0
x1[x1>5]=1
print(x1)
print('****************************************')
x2 = np.arange(20).reshape(4,5)
print(x2>10)
print(x2[x2>10])
#eg:以每一行的最后一个数为判别依据修改此行所有的值
x2[x2[:,-1]>10] = 666
print(x2)
#条件组合
#eg:取偶数或大于7的数
x = np.arange(10)
print(x[(x%2==0) | (x>7)])
标签:03,索引,x2,np,-----,数组,print,x1,10
From: https://www.cnblogs.com/lyc2001/p/17062964.html