我想从列表中替换大纲。因此我定义了一个上限和下限。现在,上面upper_bound
和下面的每个值lower_bound
都替换为绑定值。我的方法是使用 numpy 数组分两步执行此操作。
现在我想知道是否可以一步完成,因为我猜它可以提高性能和可读性。
有没有更短的方法来做到这一点?
import numpy as np
lowerBound, upperBound = 3, 7
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[arr > upperBound] = upperBound
arr[arr < lowerBound] = lowerBound
# [3 3 3 3 4 5 6 7 7 7]
print(arr)