Type Related Errors 15.15


#1

I have been searching and i cant seem to understand what i am doing incorrectly. I was previously dealing with the error:
‘int’ object not iterable

I believe this was caused by incorrectly using the sort function. However now i am receiving the following error:
‘object of type ‘NoneType’ has no len()’

I am worried since this is a big miss on my part. I have no clue what i am doing wrong. I can’t figure out how i am incorrectly using data types?

Here is my code:

def median(inputlist):
sortedlist = inputlist.sort()
count = len(sortedlist)
low = float(sortedlist[count/2])
high = float(sortedlist[((count/2)+1)])

if count % 2:
return (high + low)/2
else:
return low


#2

Here is also a screenshot with comments so it may be easier to read. I am running the code in a Python shell experimenting when i receive the errors:

image


#3

In Python 3 this will be a float, but in Python 2 it is an integer. Explicit declaration is required for this to yield a float.

 return float(high + low) / 2

Yes, and no. Not incorrect as much as redundant.

y = x.sort()

Consider that x is a reference object that is sorted in place. y is now a reference to that object, not a copy of it. In other words, if we do an in place sort, y is not needed. Just use x.

y = sorted(x)

In the above, y is an independent object, a sorted copy of x. x is unchanged. This is probably best since it means the object in calling scope is unchanged.

data = range(50, 0, -3) + range(50, 0, -5)
print median(data)

In this, the calling scope the data is unsorted. it might be necessary to retain order of the data in the original sample list so we would not want our function to modify its order. An in-place sort in the function will take place on the caller object so its original order will be lost.

>>> def median(x):
    x.sort()
    pass

>>> d = range(50, 0, -3) + range(50, 0, -5)
>>> median(d)
>>> d
[2, 5, 5, 8, 10, 11, 14, 15, 17, 20, 20, 23, 25, 26, 29, 30, 32, 35, 35, 38, 40, 41, 44, 45, 47, 50, 50]
>>> 
>>> def median(x):
    y = sorted(x)
    pass

>>> d = range(50, 0, -3) + range(50, 0, -5)
>>> median(d)
>>> d
[50, 47, 44, 41, 38, 35, 32, 29, 26, 23, 20, 17, 14, 11, 8, 5, 2, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
>>> 

#4

Thank you mtf! This is all extremely helpful, I am still however experiencing an error when i enter a list containing only the value 1. Do you know why this is? A normal list containing multiple values works correctly

image

And here is the Error I am receiving: “IndexError : list index out of range”

this kind of makes sense since the formulas for high & low will not = 0 (the index of a single value). However I also included an if statement that said:

if count == 1:
    low = 0

and I still received the error, this is confusing since 1 % 2 == 0: is false, so it should go to the else statement and return index 0


#5

This is the revised code i thought would do the trick:

image


#6

Recall that because of zero-indexing, len(x) / 2 is the UPPER index when the length is even.

s = sorted(sample)
n = len(s)
m = int(n / 2)

m above will be the index of the median in an odd length list, or the upper index in an even length list. Therefore,

if n % 2: return s[m]
return float(s[m-1] + s[m]) / 2

Note the minus 1, as opposed to plus 1. The median index of a list with only one element will be 0, and since the length is odd, it will return s[0].


#7

this did thank trick, thank you !!!

:grinning:


#8

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