我正在尝试合并排序算法,但收到此类型错误:
Input #1: [3, 1, 2, 5, 9, 6, 7]
Traceback (most recent call last):
File "Lab1.py", line 24, in <module>
print('Output #1: ' + mergesort(a))
File "Lab1.py", line 18, in mergesort
left = mergesort(lst[:middle])
File "Lab1.py", line 20, in mergesort
return merge(left, right)
File "Lab1.py", line 6, in merge
while i < len(left) and j < len(right):
TypeError: object of type 'NoneType' has no len()
...在此代码上:
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
def mergesort(lst):
if len(lst) <= 1:
return lst
middle = int(len(lst) / 2)
left = mergesort(lst[:middle])
right = mergesort(lst[middle:])
return merge(left, right)
a = [3,1,2,5,9,6,7]
print 'Input #1: ' + str(a)
print 'Output #1: ' + mergesort(a)