Re: [Tutor] creating a range above & below a given number

2009-06-09 Thread Gonzalo Garcia-Perate
Kent thanks again for your input, rest assured is just the haste of my responses that goes unchecked :) many thanks, Gonzalo. 2009/6/9 Kent Johnson : > On Mon, Jun 8, 2009 at 12:56 PM, Gonzalo > Garcia-Perate wrote: >> Kent, Emile thank you both. You're absolutely right, I was going for >> range

Re: [Tutor] creating a range above & below a given number

2009-06-08 Thread Kent Johnson
On Mon, Jun 8, 2009 at 12:56 PM, Gonzalo Garcia-Perate wrote: > Kent, Emile thank you both. You're absolutely right, I was going for > range because I thought it made the code more readable/more explicit. > I hadn't taken into account the performance hit of creating the list > and iterating through

Re: [Tutor] creating a range above & below a given number

2009-06-08 Thread Gonzalo Garcia-Perate
Kent, Emile thank you both. You're absolutely right, I was going for range because I thought it made the code more readable/more explicit. I hadn't taken into account the performance hit of creating the list and iterating through it. I'm not sure it was more readable either. the function now reads

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Kent Johnson
On Sun, Jun 7, 2009 at 3:34 PM, Emile van Sebille wrote: > To explain why we've tended to suggest using int and minval tests and avoid the range inclusion test, consider the following timeit > results: The performance penalty for 'in range(...)' is even greater when the value is not found, becaus

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Emile van Sebille
On 6/7/2009 10:17 AM Gonzalo Garcia-Perate said... Emile, Kent thank you both for your reply, after sending my previous email I realised that it wasn't working as expected in all cases. this does work: def within_range_final(self, n, n2, threshold=5): return n in range(n2-threshold, n2+thr

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Kent Johnson
On Sun, Jun 7, 2009 at 1:17 PM, Gonzalo Garcia-Perate wrote: > Emile, Kent thank you both for your reply, after sending my previous > email I realised that it wasn't working as expected in all cases. > > this does work: > > def within_range_final(self, n, n2, threshold=5): >     return n in range(n

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Gonzalo Garcia-Perate
Emile, Kent thank you both for your reply, after sending my previous email I realised that it wasn't working as expected in all cases. this does work: def within_range_final(self, n, n2, threshold=5): return n in range(n2-threshold, n2+threshold+1) What I have is an ultrasound sensor that g

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Kent Johnson
On Sun, Jun 7, 2009 at 10:08 AM, Gonzalo Garcia-Perate wrote: >  the solution laid somewhere in between: > > def within_range(self, n, n2, threshold=5): >        if n in range(n2-threshold, n2+threshold+1) and n < > n2+threshold or n > n2 + threshold : return True >        return False This is a b

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Emile van Sebille
On 6/7/2009 8:44 AM Emile van Sebille said... On 6/7/2009 7:08 AM Gonzalo Garcia-Perate said... the solution laid somewhere in between: def within_range(self, n, n2, threshold=5): if n in range(n2-threshold, n2+threshold+1) and n < n2+threshold or n > n2 + threshold : return True

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Emile van Sebille
On 6/7/2009 7:08 AM Gonzalo Garcia-Perate said... the solution laid somewhere in between: def within_range(self, n, n2, threshold=5): if n in range(n2-threshold, n2+threshold+1) and n < n2+threshold or n > n2 + threshold : return True return False Be careful here -- you proba

Re: [Tutor] creating a range above & below a given number

2009-06-07 Thread Gonzalo Garcia-Perate
the solution laid somewhere in between: def within_range(self, n, n2, threshold=5): if n in range(n2-threshold, n2+threshold+1) and n < n2+threshold or n > n2 + threshold : return True return False This seems a bit more pythonic than my original function. Thank you both. 2009/6/

Re: [Tutor] creating a range above & below a given number

2009-06-06 Thread Alan Gauld
"Gonzalo Garcia-Perate" wrote Hello tutor, What's the simplest way of creating a list with a range of numbers above and below a given number? ... this works fine, but is there a simpler way of doing it? a one liner? :) I'd probably use a list comprehesion: L = [n for n in range(lower, uppe

Re: [Tutor] creating a range above & below a given number

2009-06-06 Thread Emile van Sebille
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 abo

[Tutor] creating a range above & below a given number

2009-06-06 Thread Gonzalo Garcia-Perate
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( media