Bit of a newbie question here. I've plotted some data in Python and I need to know where, on the x axis, the first time the corresponding y-values drop below a certain number. I want to ignore any further increases in y values.
I've tried using a zip function like:
for x_val, y_val in zip(xvalues, yvalues):
if 100 < y_val < 150:
edge1 = x_val
 But I can't seem to get my threshold values right and I would like a technique that just selects the first time the y values drop below a certain number.
Thanks. Helen
Once the Y value is below the threshold you have to break
from the loop, otherwise the loop will continue and the X value will get overwritten if the Y value is below the threshold another time.
for x_val, y_val in zip(xvalues, yvalues):
if 100 < y_val < 150:
edge1 = x_val
break
Or you could use itertools.dropwhile
to skip all the values not in that range:
next(itertools.dropwhile(lambda t: not 100 < t[1] < 150, zip(xvalues, yvalues)))
Try
for x_val, y_val in zip(xvalues, yvalues):
if 100 < y_val < 150:
edge1 = x_val
Your code doesn't work because the statement inside the if is not executed because it is not properly indented or if you want to all the x_val
which satisfies the the if statement
x=[]
for x_val, y_val in zip(xvalues, yvalues):
if 100 < y_val < 150:
x.append(x_val)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.