On 6/6/2009 12:31 PM Gonzalo Garcia-Perate said...
Hello tutor, What's the simplest way of creating a list with a range
of numbers above and below a given number?

see the function below:

    def within_range( self, number, median, threshold=5 ):
        # build a list of (threshold) numbers above and below median
        range_list = range( median - threshold, median )
        range_list.extend( range( median, median + ( threshold + 1 ) ) )
        if __debug__: print "number %s, median %s, within range %s,
list %s, list length %s" % ( number, median, number in range_list,
range_list, len( range_list ) )
        if number not in range_list: return False
        return True

this works fine, but is there a simpler way of doing it? a one liner?  :)


This either returns True or False depending upon whether the passed in number is within threshold of the median ... something like (untested):


def within_range(number, median, threshold=5):
    return threshold-median<number<threshold+median

Of course, the ranges you're testing with are all integers, so you may want to check for that as well.

Emile


Thanks,
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to