"""
np.where函数是三元表达式 x if condition else y的向量化版本
np.where(condition,result_1,result_2)
"""
import numpy as np
arr_1 = np.arange(32).reshape((4, 8))
print(np.where(arr_1 > 5, 4, 8)) # 数组中,小于5的全替换成4,大于5的全替换成8
"""
[[8 8 8 8 8 8 4 4]
[4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4]]
"""
一、Numpy-10.条件逻辑操作数组
最新推荐文章于 2022-02-28 13:41:58 发布