I am working with python 2.7 and want to do some post-processing for Abaqus odb.
I have a tuple which looks like following :
tuple_1= ( (x1, y1), (x2, y2), (xfind, ywant), .. (xend, yend) )
And I want to find the first corresponding x, xfind, to a given value of y, ywant. I tried to work with '.index(ywant)' with the aim that it returns xfind, but it wasn't really helpful since I need xfind too...
But maybe it is better to be more specific, as it can be maybe solved by some boolean actions (..?) too: The x values of the tuples are timesteps and the y values are either 1.0 or 0.0. I would like to go trough all y values until the value swtiches from 1.0 to 0.0 and then find the corresponding time and use both xfind and ywant in further code.
It isn't a problem for further code to transfer the tuple to a list.
Is there a way to perform this with python 2.7?
Thank you in advance!
There is a nice way using next
:
next((x for x, y in tuple_1 if y == 'ywant'), 'not found')
Code:
tuple_1= ( ('x1', 'y1'), ('x2', 'y2'), ('xfind', 'ywant'), ('xend', 'yend') )
print(next((x for x, y in tuple_1 if y == 'ywant'), 'not found'))
# xfind