How can I write this code python in one line?
num_positive_weights = 0
for i in weights['value']:
if i >= 0:
num_positive_weights+=1
Well, that's not valid Python code (the i++
syntax isn't supported), but it would be as follows:
num_positive_weights = sum(i>=0 for i in weights['value'])
num_positive_weights = len([i for i in weights['value'] if i >= 0])
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