Python - 确定 3 个范围的重叠 [英] Python - Determine overlaps of 3 ranges
本文介绍了Python - 确定 3 个范围的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于如何在不使用任何现有库的情况下确定 Python 中三个范围的重叠的问题:
例如,如果我有三个范围(10,20)(15,25)(18,30),我应该如何找到它们之间的重叠?
我的答案应该是 (18,19,20)
任何帮助将不胜感激.谢谢 !
解决方案
重叠从最高起点到最低终点:
ranges = [(10,20), (15,25), (18,30)]开始,结束 = zip(*ranges)结果 = 范围(最大(开始),最小(结束)+ 1)
测试:
<预><代码>>>>打印(*结果)18 19 20I had a question regarding how I should go about determining overlaps of three ranges in Python without using any existing libraries :
For instance if I have three ranges as (10,20)(15,25)(18,30), how should I go about finding overlaps between them ?
My answer should be (18,19,20)
Any help would be much appreciated. Thanks !
解决方案
The overlap goes from the highest start point to the lowest end point:
ranges = [(10,20), (15,25), (18,30)]
starts, ends = zip(*ranges)
result = range(max(starts), min(ends) + 1)
Test:
>>> print(*result)
18 19 20
这篇关于Python - 确定 3 个范围的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文