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-
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
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
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
>
> 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])
[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
"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
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
"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
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
"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
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
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
[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
>>> 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
(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
16 matches
Mail list logo