我有两个形式为:
的列表:lst1 = [(1.2, 4), (5, 8), (19, 21), (24.5, 26)]
lst2 = [(1, 3), (6.55, 14.871), (22, 23)]
我希望获取的输出是:
output = [(1.2, 3), (6.55, 8)]
基本上,我希望得到这两个列表中元组定义的范围的交集。
您可以假设:
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
有什么高效的方法可以实现这个目标吗?
lst1 = [(1.2, 4), (5, 8), (19, 21), (24.5, 26)]
和lst2 = [(1, 3), (6.55, 14.871), (22, 25)]
,期望的输出是[(1.2, 3), (6.55, 8), (24.5, 25)]
。但你的程序输出为[(1.2, 3), (6.55, 8)]
。 - bumblebeeoutput = [(1.2, 3), (6.55, 8)]
”。最后一个范围是无效的,因此不应出现在输出中。 - Lindalst1
的第一个元素是(0,0)
,它将与(1,3)
进行比较,并跳过(1.2,4)
与(1,3)
之间的比较以及所有其他点 - 这将给出一个空列表作为解决方案,这是不正确的。 - Ricky Kim