Python value in interval

Determine whether integer is between two other integers

How do I determine whether a given integer is between two other integers (e.g. greater than/equal to 10000 and less than/equal to 30000 )?

Check your boolean operators, of course a number will be greater than 10000 if it’s greater than 30000. Look at the little details and you will catch far more mistakes.

The last edit made on this question is just putting «the solution» into the problem code. (makes the question somewhat invalid, defeats the purpose of this post I think.)

The question clearly refers to the syntax of such comparison and has nothing to do with the number >= 30000 blunder. The edit was fine.

16 Answers 16

@MikeC With the interval comparison number is first compared against 10000 . If it’s less than 10000 the expression is immediately short-circuited and the second comparison is not checked. The complexity is O(1) . in range(0, n) instead generates the entire sequence of numbers and then iterates through it. The complexity is O(n) . The complexity of in set(range(0, n)) is still O(n) because building a set has a time complexity of O(n) ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

@MikeC Try to run in your shell: > python -m timeit ‘10000 <= 10 <= 30000' >python -m timeit ’10 in range(10000, 30001)’ > python -m timeit ’10 in set(range(10000, 30001))’

looks like in python3.5.2, range is ~10x slower than the if statement, with speed constant in regard to range check value. thus most likely difference due to function overhead.

@IanElvister That’s incorrect. range no longer creates a list in Python 3. See Why is 10**15 in range(10**15+1) so fast in Python 3?

>>> r = range(1, 4) >>> 1 in r True >>> 2 in r True >>> 3 in r True >>> 4 in r False >>> 5 in r False >>> 0 in r False 

Wow I always thought range (or xrange in python2) returns a generator thus you cannot repeatedly test on it.

Its important to so keep in mind that 4 in range(1,4) is False. So better use the 1 >= r

(1.) bad performance (as others have pointed out this syntax looks good but can take a long time to execute because it is O(n) operations vs the if a

To check that the number is in the range 10000 — 30000, use the Python interval comparison:

This Python feature is further described in the Python documentation.

You can also use it for the initial comparison, although it’s as useless: if 10000

There are two ways to compare three integers and check whether b is between a and c:

The first one looks like more readable, but the second one runs faster.

Let’s compare using dis.dis:

>>> dis.dis('a < b and b < c') 1 0 LOAD_NAME 0 (a) 2 LOAD_NAME 1 (b) 4 COMPARE_OP 0 (<) 6 JUMP_IF_FALSE_OR_POP 14 8 LOAD_NAME 1 (b) 10 LOAD_NAME 2 (c) 12 COMPARE_OP 0 (<) >> 14 RETURN_VALUE >>> dis.dis('a < b < c') 1 0 LOAD_NAME 0 (a) 2 LOAD_NAME 1 (b) 4 DUP_TOP 6 ROT_THREE 8 COMPARE_OP 0 (<) 10 JUMP_IF_FALSE_OR_POP 18 12 LOAD_NAME 2 (c) 14 COMPARE_OP 0 (<) 16 RETURN_VALUE >> 18 ROT_TWO 20 POP_TOP 22 RETURN_VALUE >>> 

and using timeit:

also, you may use range, as suggested before, however it is much more slower.

if number >= 10000 and number  

Define the range between the numbers:

if num in r: print("All right!") 

range doesn't count the last value 10 in your case . range(1,11) is correct, if you need to compare between 1 and 10

Python lets you just write what you mean in words:

if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :) 

In Python3, use range instead of xrange .

edit: People seem to be more concerned with microbench marks and how cool chaining operations. My answer is about defensive (less attack surface for bugs) programming.

As a result of a claim in the comments, I've added the micro benchmark here for Python3.5.2

$ python3.5 -m timeit "5 in range(10000, 30000)" 1000000 loops, best of 3: 0.266 usec per loop $ python3.5 -m timeit "10000  

If you are worried about performance, you could compute the range once

$ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R" 10000000 loops, best of 3: 0.0551 usec per loop 

Источник

How to check if a number is in a interval

I want an interval from second_var like second_var ± interval (from 3 to 7). I wank to check if first_var is in that interval. So in this specific case I want False If first_var = 4 , I want True I can do this:

if (first_var > second_var-interval) and (first_var < second_var+interval): #True 

If your first_var is always an integer, you can just use range/xrange objects for Python 3.x and Python 2.x respectively and combine them with the in operator for speedy membership tests that read like English. Example: 4 in range(3, 8) Note that the stop parameter is non-inclusive for the range, but you can assign the range to any variable that has a more descriptive name.

4 Answers 4

You can use math-like sequence as Python supports that

Note that comments in python begin with a #

I use a class with __contains__ to represent the interval:

class Interval(object): def __init__(self, middle, deviation): self.lower = middle - abs(deviation) self.upper = middle + abs(deviation) def __contains__(self, item): return self.lower  

Then I define a function interval to simplify the syntax:

def interval(middle, deviation): return Interval(middle, deviation) 

Then we can call it as follows:

>>> 8 in interval(middle=6, deviation=2) True >>> 8 in interval(middle=6, deviation=1) False 

With Python 2 this solution is more efficient than using range or xrange as they don't implement __contains__ and they have to search for a matching value.

Python 3 is smarter and range is a generating object which is efficient like xrange , but also implements __contains__ so it doesn't have to search for a valid value. xrange doesn't exist in Python 3.

This solution also works with floats.

Also, note, if you use range you need to be careful of off-by-1 errors. Better to encapsulate it, if you're going to be doing it more than once or twice.

Источник

Using intervals¶

This section gives an overview of the use of interval classes and examples of working with interval data.

Run the following commands to connect the necessary modules

>>> import numpy as np >>> from intvalpy import Interval, precision 

It connects the interval function Interval from the implemented IntvalPy library, which was previously installed with the command pip install intvalpy .

The class Interval was created in accordance with IEEE Standard 754-2008, where rounding occurs to the nearest even number. This allows significantly to accelerate the computational upper-level functions and reduce the computation time. However, in tasks where you need more precision, you can switch from the standard representation of the number as double float type to mpf type. To do this, run the following command:

>>> precision.extendedPrecisionQ = True 

You can also set the working precision (after which decimal place rounding will take place):

The default setting is increased accuracy to 36th decimal place.

The IntvalPy library supports classical arithmetic and full Kaucher interval arithmetic. Each arithmetic has different types of intervals (for example, classical arithmetic considers only «correct» intervals), which means that the arithmetic operations can differ from each other. Therefore, it was decided to develop two different classes for each of the arithmetics. However, there are not few common characteristics that these classes could inherit from some third class. These could be the operations of taking the ends of an interval, the width or radius, and many other things. This is why the parent class `` BaseTools““ was made separately.

Basic сharacteristics¶

class BaseTools(left, right)

A parent class that contains methods that can be used to calculate the basic interval characteristics of any interval arithmetic. Used in the ClassicalArithmetic and KaucherArithmetic classes.

  • left int, float The lower (left) limit of the interval.
  • right int, float The upper (right) limit of the interval.
  1. a, inf: The operation of taking the lower (left) bound of the interval.
  2. b, sup: The operation of taking the upper (right) bound of the interval.
  3. copy: Creates a deep copy of the interval.
  4. to_float: If increased accuracy is enabled, it is sometimes necessary to convert to standard accuracy (float64)
  5. wid: Width of the non-empty interval.
  6. rad: Radius of the non-empty interval.
  7. mid: Midpoint of the non-empty interval.
  8. mig: The smallest absolute value in the non-empty interval.
  9. mag: The greatest absolute value in the non-empty interval.
  10. dual: Dual interval.
  11. pro: Correct projection of an interval.
  12. opp: Algebraically opposite interval.
  13. inv: Algebraically opposite interval.
  14. khi: Ratschek’s functional of an interval.

Interval vectors and matrices¶

class ArrayInterval(intervals)

It is often necessary to consider intervals not as separate objects, but as interval vectors or matrices. It is important to note that different intervals can be from different interval arithmetics, which leads to additional checks when performing arithmetic operations. The IntvalPy library, using the ArrayInterval class, allows you to create such arrays of any nesting. In fact, we use arrays created using the numpy library.

This class uses all the methods of the BaseTools class, but there are additional features that are common to interval vectors and matrices.

  • intervals ndarray The numpy array with objects of type ClassicalArithmetic and KaucherArithmetic.
  1. data: An array of interval data of type ndarray .
  2. shape: The elements of the shape tuple give the lengths of the corresponding interval array dimensions.
  3. ndim: Number of interval array dimensions.
  4. ranges: A list of indexes for each dimension.
  5. vertex: The set of extreme points of an interval vector.
  6. T: View of the transposed interval array.
  7. reshape(new_shape): Gives a new shape to an interval array without changing its data.
>>> f = Interval([ [[-1, 3], [-2, 5]], [[-7, -4], [-5, 7]] ]) >>> s = Interval([ [[-3, -2], [4, 4]], [[-7, 3], [-8, 0]] ]) >>> f @ s # Interval([['[-44.0, 18.0]', '[-44.0, 28.0]'] ['[-41.0, 56.0]', '[-84.0, 24.0]']]) 
>>> f.T # Interval([['[-1, 3]', '[-7, -4]'], ['[-2, 5]', '[-5, 7]']]) 

Create intervals¶

def Interval(*args, sortQ=True, midRadQ=False)

When creating an interval, you must consider which interval arithmetic it belongs to, and how it is defined: by means of the left and right values, through the middle and radius, or as a single object. For this purpose, a universal function Interval has been implemented, which can take into account all the aspects described above. In addition, it has a parameter for automatic conversion of the ends of an interval, so that when the user creates it, he can be sure, that he works with the classical type of intervals.

  • args int, float, list, ndarray If the argument is a single one, then the intervals are set as single objects. To do this you must create array, each element of which is an ordered pair of the lower and upper bound of the interval. If the arguments are two, then the flag of the midRadQ parameter is taken into account. If the value is True , then the interval is set through the middle of the interval and its radius. Otherwise, the first argument will stand for the lower ends, and the second argument the upper ends.
  • sortQ bool, optional Parameter determines whether the automatic conversion of the interval ends should be performed. The default is True .
  • midRadQ bool, optional The parameter defines whether the interval is set through its middle and radius. The default is False .

Creating intervals by specifying arrays of left and right ends of intervals

>>> a = [2, 5, -3] >>> b = [4, 7, 1] >>> Interval(a, b) # Interval(['[2, 4]', '[5, 7]', '[-3, 1]']) 

Now let’s create the same interval vector, but in a different way

>>> Interval([ [2, 4], [5, 7], [-3, 1] ]) # Interval(['[2, 4]', '[5, 7]', '[-3, 1]']) 

In case it is necessary to work with an interval object from Kaucher arithmetic, it is necessary to disable automatic converting ends

>>> Interval(5, -2, sortQ=False) # '[5, -2]' 

As mentioned earlier, the IntvalPy library allows you to work with vectors and matrices. This automatically generates the need to calculate the length of the array, as well as the possibility of working with collections.

>>> f = Interval([ [2, 4], [5, 7], [-3, 1] ]) >>> len(f) # 3 

To get the N-th value or several values (in the future we will call it a slice of the array) you can use quite usual tools. Moreover, since the class ArrayInterval is changeable, it is also possible to change or delete elements:

>>> f[1] # [5, 7] >>> f[1:] # Interval(['[5, 7]', '[-3, 1]']) >>> f[1:] = Interval([ [-5, 5], [-10, 10] ]) >>> f # Interval(['[2, 4]', '[-5, 5]', '[-10, 10]']) >>> del f[1] >>> f # Interval([’[2, 4]’, ’[-10, 10]’]) 

© Copyright . Revision ef766a9b .

Источник

Читайте также:  Html выравнивание div внутри div
Оцените статью