Re: Mean, median, and mode

2004-12-06 Thread Steven Bethard
Paul Rubin wrote: median = x.sorted()[len(x)//2] In Python 2.4: >>> [10, 8, 6, 4, 2, 1].sorted() Traceback (most recent call last): File "", line 1, in ? AttributeError: 'list' object has no attribute 'sorted' Perhaps you meant "sorted(x)"? Steve -- http://mail.python.org/mailman/listinfo/python-

Re: Mean, median, and mode

2004-12-06 Thread Fredrik Lundh
Paul Rubin wrote: > > > median = lambda x: x.sort() or x[len(x)//2] > > Yucch!! > > median = x.sorted()[len(x)//2] that doesn't do the same thing, and doesn't work on pre-2.4 releases. try again. (learning how "or" and "and" works in Python doesn't hurt, either; "value or value" is a very commo

Re: Mean, median, and mode

2004-12-06 Thread Paul Rubin
Josiah Carlson <[EMAIL PROTECTED]> writes: > > > median = lambda x: x.sort() or x[len(x)//2] > > > > That...is a really sneaky use of null return values. I like. :) > > Thank you, I'm just using a paradigm (exploiting lambdas) that I picked > up while going through various functional programming

Re: Mean, median, and mode

2004-12-06 Thread Scott David Daniels
How about: def median(lst): lst = sorted(lst) inner = lst[(len(lst) - 1) // 2 : (len(lst) + 2) // 2] if len(inner) > 1: return sum(inner) / 2. else: return inner[0] --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mail

RE: Mean, median, and mode - third time's the charm!!!

2004-12-06 Thread Frohnhofer, James
> > median = lambda x: x.sort() or ((len(x) % 2) and (x[(len(x)>>1)])) or > (sum(x[((len(x)>>1)-1):(len(x)>>1)+1])/2.0) > > >>> median( [2,3,4,5,6,7]) > 4.5 > >>> median( [2,3,4,5,6]) > 4 How about >>> median = lambda x: x.sort() or (x[(len(x)-1)/2] + x[len(x)/2])/2.0 >>> median([2,3,4,5,6,7])

Re: Mean, median, and mode

2004-12-06 Thread Josiah Carlson
[EMAIL PROTECTED] (Michael Fuhr) wrote: > > Josiah Carlson <[EMAIL PROTECTED]> writes: > > > > "Robert Brewer" <[EMAIL PROTECTED]> wrote: > > > > > > Josiah Carlson wrote: > > > > > > > > median = lambda x: x.sort() or x[len(x)//2] > > > > > > That...is a really sneaky use of null return val

Re: Mean, median, and mode - third time's the charm!!!

2004-12-06 Thread Paul McGuire
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > "Paul McGuire" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > This median expression is incorrect. median is *not* the midpoint between > > max and min values. It is the middle value when all values

Re: Mean, median, and mode

2004-12-06 Thread Peter Hansen
Michael Fuhr wrote: Josiah Carlson <[EMAIL PROTECTED]> writes: "Robert Brewer" <[EMAIL PROTECTED]> wrote: Josiah Carlson wrote: median = lambda x: x.sort() or x[len(x)//2] That...is a really sneaky use of null return values. I like. :) Thank you, I'm just using a paradigm (exploiting lambdas) that

Re: Mean, median, and mode

2004-12-06 Thread Paul McGuire
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > This median expression is incorrect. median is *not* the midpoint between > max and min values. It is the middle value when all values are sorted (for > an odd number of values), or the average of the two middle values

Re: Mean, median, and mode

2004-12-06 Thread Michael Fuhr
Josiah Carlson <[EMAIL PROTECTED]> writes: > "Robert Brewer" <[EMAIL PROTECTED]> wrote: > > > > Josiah Carlson wrote: > > > > > > median = lambda x: x.sort() or x[len(x)//2] > > > > That...is a really sneaky use of null return values. I like. :) > > Thank you, I'm just using a paradigm (exploi

Re: Mean, median, and mode

2004-12-06 Thread Josiah Carlson
"Robert Brewer" <[EMAIL PROTECTED]> wrote: > > Josiah Carlson wrote: > > [EMAIL PROTECTED] (Sean McIlroy) wrote: > > > > > > >>> median = lambda x: (max(x)-min(x))/2 > > > > That is /not/ the median in the general case. > > > > median = lambda x: x.sort() or x[len(x)//2] > > That...is a reall

Re: Mean, median, and mode

2004-12-06 Thread Paul McGuire
This median expression is incorrect. median is *not* the midpoint between max and min values. It is the middle value when all values are sorted (for an odd number of values), or the average of the two middle values when all values are sorted (for an even number of values). In Python 2.4 (needed

RE: Mean, median, and mode

2004-12-06 Thread Robert Brewer
Josiah Carlson wrote: > [EMAIL PROTECTED] (Sean McIlroy) wrote: > > > > >>> median = lambda x: (max(x)-min(x))/2 > > That is /not/ the median in the general case. > > median = lambda x: x.sort() or x[len(x)//2] That...is a really sneaky use of null return values. I like. :) Robert Brewer MIS

Re: Mean, median, and mode

2004-12-06 Thread Josiah Carlson
[EMAIL PROTECTED] (Sean McIlroy) wrote: > > >>> median = lambda x: (max(x)-min(x))/2 That is /not/ the median in the general case. median = lambda x: x.sort() or x[len(x)//2] - Josiah -- http://mail.python.org/mailman/listinfo/python-list

Re: Mean, median, and mode

2004-12-05 Thread Sean McIlroy
>>> mean = lambda x: sum(x)/len(x) >>> median = lambda x: (max(x)-min(x))/2 >>> mode = lambda x: max([(x.count(y),y) for y in x])[1] "Robert Brewer" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>... > (now that we have a meaningful subject line) > > Alfred Canoy wrote: > > >> I'm

RE: Mean, median, and mode

2004-12-05 Thread Robert Brewer
(now that we have a meaningful subject line) Alfred Canoy wrote: > >> I'm just new to programming and would like to ask for help.. > >> > >> Build a module that contains three functions that do the following: > >> > >> a.. Compute the average of a list of numbers > >> b.. Finds the st