我無法克服此錯誤。我正在做一個類似mergesort合併的例程。也許我正在實例化錯誤。下面的代碼:Python IndexError:查看if語句時列表索引超出範圍
def MergeAndCountSplitInv(arr, length):
left = arr[0:length/2]
right = arr[length/2+1: length]
i = 0
j = 0
numSplitInv = 0
newArray = [0] * length
for k in range(len(arr)):
if (left[i] < right[j]):
#newArray.insert(k, left[i])
newArray.append(left[i])
i = i + 1
else: #(right[j] < left[i]
#newArray[k].insert(k, right[j])
newArray.append(right[j])
j = j + 1
numSplitInv = numSplitInv + (length/2 - i)
return ReturnValue(newArray, numSplitInv)
誤差給出如下:
File "InversionCount.py", line 31, in MergeAndCountSplitInv if (left[i] < right[j]): IndexError: list index out of range
感謝您的幫助