NumPy笔记:逻辑运算符与三元运算符
import numpy as np print("-------------逻辑运算符---------------") a = np.array([1, 2, 3, 4, 2, 3, 2, 4, 6, 3, 5]) print(a > 4) a[a > 4] = 4 print(a) print("-------------三元运算符---------------") print(np.where(a > 4, 0, 1)) print(np.where(np.logical_and(a > 1, a < 3), 0, 1)) print(np.where(np.logical_or(a > 1, a < 3), 0, 1))
-------------逻辑运算符--------------- [False False False False False False False False True False True] [1 2 3 4 2 3 2 4 4 3 4] -------------三元运算符--------------- [1 1 1 1 1 1 1 1 1 1 1] [1 0 1 1 0 1 0 1 1 1 1] [0 0 0 0 0 0 0 0 0 0 0] ————————————————