numpy1.1: np.where用法、及其相似搜索清洗写法

方法:

# 用法1
# np.where(condition,x,y)      满足条件,输出x,不满足输出y

# 用法2
# np.where(condition)      返回满足条件的元素坐标

np.where示例代码

'''
import numpy as np
arr = np.arange(1,10,1).reshape(3,3)
print(arr)

# 用法1
arr1 = np.where((8>=arr)&(arr>=5),arr*2,0)
print(arr1)

# 用法2
position = np.where(arr>=5)     # 返回坐标,类型元组:(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2])),意思是(1,1)(1,2)(2,0)(2,1)(2,2)
list0 = position[0]  # 得到元组的第一个列表,行坐标
list1 = position[1]  # 得到元组的第一个列表,行坐标

for i in range(len(list0)):
    x = arr[list0[i],list1[i]]
    print(x)

numpy自带方法,示例:

import numpy as np
b = np.arange(9).reshape(3, 3)
print(b)

print(b[b>5])                      
print(b[(b > 3) & (b< 6)])      # 多个判断条件:一定要记得 分开写、加括号

# 尤其注意这一行

猜你喜欢

转载自blog.csdn.net/qq_43328166/article/details/108297944