如何在python中加入整数间隔? [英] How to join integers intervals in python?

查看:198
本文介绍了如何在python中加入整数间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了模块间隔( http://pyinterval.readthedocs.io/en /latest/index.html )

I have used the module intervals (http://pyinterval.readthedocs.io/en/latest/index.html)

并从集合或开始,结束元组创建间隔:

And created an interval from a set or start, end tuples:

intervals = interval.interval([1,8], [7,10], [15,20])

当[1,8]和[7,10]重叠时,会导致间隔([1.0,10.0],[15.0,20.0]).

Which result in interval([1.0, 10.0], [15.0, 20.0]) as the [1,8] and [7,10] overlaps.

但是此模块将对的值解释为实数,因此整数中的两个连续间隔将不会连接在一起.

But this module interprets the values of the pairs as real numbers, so two continuous intervals in integers will not be joined together.

示例:

intervals = interval.interval([1,8], [9,10], [11,20]) 

结果为:interval([1.0,8.0],[9.0,10.0],[11.0,20.0])

results in: interval([1.0, 8.0], [9.0, 10.0], [11.0, 20.0])

我的问题是如何将这个间隔作为整数而不是实数加入?在最后一个示例中,结果将是interval([1.0,20.0])

My question is how can I join this intervals as integers and not as real numbers? And in the last example the result would be interval([1.0, 20.0])

推荐答案

时间间隔模块 pyinterval 用于实数,而不用于整数.如果要使用对象,则可以创建一个整数间隔类,或者也可以使用间隔模块对程序进行编码以加入整数间隔:

The intervals module pyinterval is used for real numbers, not for integers. If you want to use objects, you can create an integer interval class or you can also code a program to join integer intervals using the interval module:

def join_int_intervlas(int1, int2):
    if int(int1[-1][-1])+1 >= int(int2[-1][0]):
        return interval.interval([int1[-1][0], int2[-1][-1]])
    else:
        return interval.interval()

这篇关于如何在python中加入整数间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆