Results 1 to 5 of 5

Thread: Median in a list

  1. #1
    Join Date
    Dec 2011
    Posts
    142
    Mentioned
    0 Post(s)
    Quoted
    12 Post(s)

    Default Median in a list

    Hey guys i'm trying to find the median in a list of user inputs and i'm not quite sure where i am going wrong.

    Code:
    def median(alist):
        s_list = sorted(alist)
        l = len(alist)
        if l % 2:
            return s_list[1 // 2]
        else:
            return (s_list[1 // 2] + s_list[1 // 2 - 1]) / 2.0
    When I run the code I get this

    Code:
    median([7,12,3,1,6])
    1
    Any help is much appreciated. Thanks.

  2. #2
    Join Date
    Dec 2013
    Location
    Pitcairn Island
    Posts
    288
    Mentioned
    20 Post(s)
    Quoted
    166 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    Any help is much appreciated. Thanks.
    python Code:
    def median(alist):
        s_list = sorted(alist)
        l = len(alist)
        if l % 2:
            return s_list[((l+1)/2)-1]
        else:
            return float(sum(s_list[(l/2)-1:(l/2)+1]))/2.0


    print(median([1, 2, 3, 4, 5]))
    print(median([1, 2, 3, 4]))

    Code:
    3
    2.5

  3. #3
    Join Date
    Dec 2011
    Posts
    142
    Mentioned
    0 Post(s)
    Quoted
    12 Post(s)

    Default

    Quote Originally Posted by TSN View Post
    python Code:
    def median(alist):
        s_list = sorted(alist)
        l = len(alist)
        if l % 2:
            return s_list[((l+1)/2)-1]
        else:
            return float(sum(s_list[(l/2)-1:(l/2)+1]))/2.0


    print(median([1, 2, 3, 4, 5]))
    print(median([1, 2, 3, 4]))

    Code:
    3
    2.5
    For some reason this isn't working for me. But looking at your code helped me manage to figure out what was wrong with mine. I was using 1 instead of l in some places. The two are far too close in my newbie opinion.

    Anyways, thanks a bunch mate.

  4. #4
    Join Date
    Jan 2013
    Posts
    146
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    For some reason this isn't working for me. But looking at your code helped me manage to figure out what was wrong with mine. I was using 1 instead of l in some places. The two are far too close in my newbie opinion.

    Anyways, thanks a bunch mate.
    It's probably a good time to start breaking the habit of using lowercase L as a variable name or other letters that can be easily confused like an Upper case O could be easily confused with a number 0

    Im Back... Previously known as Megaleech
    [Herbalife]

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    For some reason this isn't working for me. But looking at your code helped me manage to figure out what was wrong with mine. I was using 1 instead of l in some places. The two are far too close in my newbie opinion.

    Anyways, thanks a bunch mate.
    Reason for it not working in Python 3; "/" is float division. While "//" is integer division, you can NOT index with "arr[x/2]", as then the index would be a float.
    To avoid issues like this for any future question it helps to mention you are using Python 3, else you will likely get 2.7 answer that might not work in Python 3. Py2.7 is still where much of the python community is.

    python Code:
    def median(lst):
        lst = sorted(lst)
        med,rem = divmod(len(lst), 2)
        if not rem:
          return (lst[med] + lst[med-1]) / 2.0
        return lst[med]
           

    print(median([1, 2, 3, 4, 5]))
    print(median([1, 2, 3, 4]))
    Would be an alternative solution. Using an if expression for the result would also work here, but that makes it less readable for newcommers.

    If you are using Python 3.4+ (i think it was) then it's just an import of https://docs.python.org/3/library/st...tistics.median

    Quote Originally Posted by Saint//+ View Post
    It's probably a good time to start breaking the habit of using lowercase L as a variable name or other letters that can be easily confused like an Upper case O could be easily confused with a number 0
    For some reaosn this isn't an issue I have ever experienced in all my years of programming. Just keep in min pythons styleguide: Normally this means that words that uses "PascalCase" is classnames, variables follow "under_score" or "camelCase", so "I" would not be a suitable variable name, "I" however could be a horribly nameed constant, which is all uppercase, luckily this never happens.
    Last edited by slacky; 02-20-2016 at 11:16 AM.
    !No priv. messages please

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •