The appearance of the python range in another range

How can I do something like this:

>>> xrange(4, 10) in xrange(3, 20)
TRUE
+3
source share
4 answers

How about (min1 >= min2) and (max1 <= max2)?

(Assuming min1, max1 = 4, 10and min2, max2 = 3, 20)

Note. You want to compare endpoints without actually making / not evaluating ranges, otherwise it will be terribly inefficient.

edit: This also works; not better but prettier imo:min2 <= min1 <= max1 <= max2

+5
source

If you are looking for one set contained in another set, try:

>>> set(xrange(4, 10)).issubset(set(range(3,20))

If you want to compare endpoints, since you will always use ranges for this, you can simply compare endpoints such as @ zoli2k.

[EDIT] Editing requested.

+4
source
 >>>min(xrange(4, 10)) > min(range(3, 20)) and max(xrange(4, 10)) < max(range(3, 20))
 True
0

, :

>>> a = range(10)
>>> b = range(5,15)
>>> c = range(15,25)
>>> any(x in a for x in b)
True
>>> any(x in a for x in c)
False

, (100+ ) , , a "" . :.

>>> a = set(range(10))

, in .

0
source

Source: https://habr.com/ru/post/1754389/


All Articles