天天看点

python(索引的简单使用)

1.判断array中数组元素的关系:

vector = np.array([5, 10, 15, 20])
vector == 10

array([False,  True, False, False])
           

2.返回具体的值:

vector = np.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print (equal_to_ten)
print(vector[equal_to_ten])
           

可以看到equal就是对应得索引,直接输出就可以vector[equal].

3.返回具体某一列符合条件的索引:

matrix = np.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
# print(matrix[:,1])
second_column_25 = (matrix[:,1] == 25)|(matrix[:,1] == 10)
print (second_column_25)
print(matrix[second_column_25, :])

[ True  True False]
[[ 5 10 15]
 [20 25 30]]