Median - list index out of range


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


Title is fairly self explanatory. This code is giving me a 'list index out of range' error, on the test list [4, 5, 5, 4]


def median(numlist):
    sortlist = sorted(numlist)
    if len(sortlist) % 2 == 0:
        inone = sortlist[len(sortlist) / 2]
        intwo = sortlist[inone - 1]
        return inone * intwo / 2
    else:
        if len(sortlist) != 1:
            return sortlist[(len(sortlist) / 2) + 0.5]
        else:
            return sortlist[0]


#2

This might be self-explanatory, but I don't see it that way. Could you explain how inone - 1 is an index?

And how is,

a median?


#3

When you are dividing by 2, Python is forcing the answer to an integer. The expected result is float, so try dividing by a float (i.e. 2.0).


#4

As I understand it-and I could absolutely be wrong-inone becomes the index at [len(sortlist)/2], and so if I subtract one from inone, it then gets the index directly under it.

As for how inone * intwo / 2 is a median, it's taking the average of both values, which (ideally) should be the middle values, thus being the median of the number.


#5

No, it actively gets upset when I try to use a float, at least in the indeces (if that's what you're referring to)

Making the end result a float didn't help either, it's still giving me a list index out of range error.


#6

We take an average of values by adding them up, then dividing the total by the number of terms. You are multiplying, which is wildly different.

4 + 5 = 9
4 * 5 = 20

The size of the sample space has a bearing on where the middle term (or terms) will be. Since the list is zero-indexed, the middle will be,

ss = sorted(sample_space)
n = len(ss)
m = int(n / 2)

The value of m is the middle index of an odd length list. But if we look at an even length list...

0 1 2 3 4 5 6 7

n = 8
m = 4

You are on the right track as far as the index of the other element in the middle pair, it is indeed, m - 1. In your code you are assigning the value at index m, minus 1 to the other index, rather than m - 1.


#7

We take an average of values by adding them up, then dividing the total by the number of terms. You are multiplying, which is wildly different.

4 + 5 = 9
4 * 5 = 20

The size of the sample space has a bearing on where the middle term (or terms) will be. Since the list is zero-indexed, the middle will be,

ss = sorted(sample_space)
n = len(ss)
m = int(n / 2)

The value of m is the middle index of an odd length list. But if we look at an even length list...

0 1 2 3 4 5 6 7

n = 8
m = 4

You are on the right track as far as the index of the other element in the middle pair, it is indeed, m - 1.

inone is, as earlier mentioned a value, not an index.


#8

No you are correct, the indices have to be integers. I was referring to the return line, inone * intwo / 2. This is forcing that result to be in integer (4) instead of a float (4.5) which is the expected result. Try (inone * intwo) / 2.0

Edit, also as above it should be addition not multiplication to get the average of the two numbers.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.