0
我該如何解決這個問題?python3'非類型對象'不可自訂
def median(lst):
lst_s=lst.sort()
k=len(lst)
if k%2==0:
return ((lst_s[k/2]+lst_s[k/2-1])/2)
elif k%2==1:
return (lst_s[k/2-1/2])
res = median([1,2,3,7,6,5])
print('median:',res)
,當我運行它,它說
Traceback (most recent call last):
File "E:\lab5_p4_2015123176.py", line 13, in <module>
res = median([1,2,3,7,6,5])
File "E:\lab5_p4_2015123176.py", line 7, in median
return ((lst_s[k/2]+lst_s[k/2-1])/2)
TypeError: 'NoneType' object is not subscriptable
可能重複[爲什麼「返回list.sort()」返回None,不是列表?](http://stackoverflow.com/questions/7301110/why-does-return-list-sort-return-無 - 不 - 的列表) –