Is there a numpy way of doing
n = [x-t if x > 0 else x for x in nps]
similar to this
n = np.array(a)
n[np.abs(n) < t] = 0
something like this perhaps?
n[n > 0] = n-t
You can even even use Pandas Series or Numpy Arrays with List Comprehension.
b. if..else in List Comprehension in Python. You can also use an if-else in a list comprehension in Python. Since in a comprehension, the first thing we specify is the value to put in a list, this is where we put our if-else.
List comprehensions on tiny lists are faster than doing the same with numpy as the performance gain from using numpy is not enough to offset the overhead of creating an array.
Answer. Yes, an else clause can be used with an if in a list comprehension. The following code example shows the use of an else in a simple list comprehension.
Can't test now, but try
np.where(n > 0, n - t, n)
See documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With