![](/img/trans.png)
[英]How to fix “IndexError: list assignment index out of range” error in Python?
[英]How to fix 'IndexError: list index out of range' error
我是一個初學者,學習過python和入門算法,並試圖實現所學知識。 我正在嘗試以下代碼,但它反復給我“ IndexError:列表索引超出范圍”。 它在分區功能時失敗,特別是在array [0],array [pivot] = array [pivot],array [0]代碼處失敗。 我無法修復它。 任何幫助表示贊賞。
from math import floor
def mergeSort(a): # mergesort function
if len(a)<2: # if length of a < 2, return a
return a
mid=int(len(a)/2) # mid point
x=mergeSort(a[:mid]) # recursivly call mergesort for 0 to mid
y=mergeSort(a[mid:]) # recursivly call mergesort from mid to end
result=[] # empty list
i=j=0 # initialize i and j
while j<len(x) and i<len(y):
if x[j]<y[i]: # if x[j] < y[i], then append result for x[j]
result.append(x[j])
j+=1 # increment j by 1
else:
result.append(y[i]) # append result for y[i]
i+=1 # increament i by 1
result+=x[j:] # add x[j:] --> result
result+=y[i:] # add y[i:] --> result
return result # return the result
def findMedian(a): # find the median
return mergeSort(a)[floor(len(a)/2)] # call mergesort
def choosePivot(a): # choose pivot
medians=[] # empty list
j=0 # initialize j
if len(a)==1: # if the len = 1, print the element
print (a[0])
return a[0]
if 5<len(a):
medians.append(findMedian(a[0:5])) # call findMedian for 0 to 5
else:
medians.append(findMedian(a)) # call findMedian for a
for i in range(1,floor(len(a)/5)): # divide the input array into 5 groups
if i*5<len(a):
medians.append(findMedian(a[j*5:i*5])) # call findMedian
else:
medians.append(findMedian(a[j*5:len(a)]))
return choosePivot(medians) # return choosePivot medians
def partition(array,pivot): # partition
array[0],array[pivot]=array[pivot],array[0] #swap
j=1 # intiatalize
for i in range(1,len(array)):
if array[i]<array[0]:
array[i],array[j-1]=array[j-1],array[i] #Swap the number less than the pivot
j+=1
array[1],array[j]=array[j],array[1] #swap the pivot to its rightful place
return j-1,array #return index of pivot and the partitioned array
def Selection(array,k): # selection function
p=choosePivot(array)
if k>len(array): # if k > length of array, then return -1
print ("Out of array index")
return -1
if len(array)>1: # if len(array) >1, then
x,b=partition(array,p) # call partition func
if x>k:
c = Selection(b[:x],k) # search the left half for the statistic
return c # return c
elif x<k:
d= Selection(b[x+1:],k-x-1) # search the right half for the statistic
return d # return d
else:
return array[k] # return the element if statistic is found
else:
return array[0] #Only one element. Only solution, return as it is.
print (Selection([5,1,48,6,2,4,8,7,5,63,2,1,4,8,99],13))
如何解決“索引超出范圍”錯誤? 調試。 熱門閱讀: 如何調試小型程序(#1) 。 使用打印語句或更好的調試器可以在代碼中的某些位置停止並檢查出了什么問題。 我為此使用Visual Studio。
紅點是一個斷點 -每當代碼碰到紅點時,它就會停止執行,我可以檢查所有內容。 然后,我可以逐行前進。 黃色箭頭顯示我所在的行。
VS可以將變量固定為源代碼的覆蓋物-參見圖像右側的小塊。
調試工具列表: https : //wiki.python.org/moin/PythonDebuggingTools
當您的程序第三次通過VS時,它碰到了def partition(array,pivot):
它超出范圍:
原因是您的pivot
包含value
而不是您需要交換它的索引。
即使您將其修復為:
def partition(array,pivot): # partition
idx = array.index(pivot) # get the index of the value here
array[0],array[idx]=array[idx],array[0] #swap
j=1 # intiatalize
for i in range(1,len(array)):
if array[i]<array[0]:
array[i],array[j-1]=array[j-1],array[i] #Swap the number less than the pivot
j+=1
array[1],array[j]=array[j],array[1] #swap the pivot to its rightful place
return j-1,array #return index of pivot and the partitioned array
交換array[1],array[j]=array[j],array[1]
時遇到另一個錯誤array[1],array[j]=array[j],array[1]
原因是j
太大:
您需要修復算法。
HTH
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.