Python 将函数作为参数传递到另一个函数中
在Python 将函数作为参数传递到另一个函数中,python,function,quicksort,Python,Function,Quicksort,在pivot2函数中,我得到了一个错误arr name未定义,当我从函数工作的参数中删除end=len(arr)时,为什么我不能传递len(arr)作为Python中的参数?因为您将arr作为参数传递,而它不是变量,Python将尝试查找引发该错误的变量 要解决此问题,而不是将len(arr)放在参数中,请将其放在实际函数中 def pivot2(arr,start=0,end=len(arr)): pivot=arr[start] swapid=start for x
pivot2
函数中,我得到了一个错误arr name未定义
,当我从函数工作的参数中删除end=len(arr)
时,为什么我不能传递len(arr)
作为Python中的参数?因为您将arr作为参数传递,而它不是变量,Python将尝试查找引发该错误的变量
要解决此问题,而不是将len(arr)放在参数中,请将其放在实际函数中
def pivot2(arr,start=0,end=len(arr)):
pivot=arr[start]
swapid=start
for x in range(start+1,end):
if(pivot>arr[x]):
swapid +=1
swap(arr,swapid,x)
print(arr)
swap(arr,start,swapid)
return swapid
def swap(arr,i,j):
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
def pivot2(arr,start=0):
end=len(arr)
pivot=arr[start]
swapid=start
for x in range(start+1,end):
if(pivot>arr[x]):
swapid +=1
swap(arr,swapid,x)
print(arr)
swap(arr,start,swapid)
return swapid