I have two lists of the form:
lst1 = [(1.2, 4), (5, 8), (19, 21), (24.5, 26)]
lst2 = [(1, 3), (6.55, 14.871), (22, 23)]
The output I am looking to get is:
output = [(1.2, 3), (6.55, 8)]
Basically, I want the intersections between the ranges defined by the tuples across the two lists.
You can assume-
the indices to be ordered within a given list. For example, in lst2:
1 < 6.55 < 22
the ranges to be valid (within a tuple, the startVal<=endEndVal). In lst2:
1 < 3 and 6.55 < 14.871 and 22 < 23
What is an efficient way to achieve this?
I think the best way to do this is with a list comprehension, given that both lists are the same length.
in two lists for readability:
# get the min max ranges
a = [(max(i[0], j[0]),min(i[1],j[1])) for i,j in zip(lst1, lst2)]
# check that min is smaller than max
a = [(i,j) for (i,j) in a if i < j]
or in one list:
a = [(i,j) for (i,j) in [(max(i[0], j[0]),min(i[1],j[1])) for i,j in zip(lst1, lst2)] if i < j]
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