我想在Python中爲快速排序實現分區功能。爲什麼「i,j = 1」導致「int ..不可迭代」?
def partition(ls):
if len(ls) == 0:
return
pivot = ls[0]
i, j = 1
while j < len(ls):
if ls[j] <= pivot:
i += 1
temp = ls[i]
ls[i] = ls[j]
ls[j] = temp
j += 1
ls[0] = ls[i]
ls[i] = pivot
但是,當我撥打quicksort.partition([1,2,3])
時,Python會發出此錯誤。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "quicksort.py", line 5, in partition
i, j = 1
TypeError: 'int' object is not iterable
這個錯誤說的是什麼?當然,int對象是不可迭代的,但是我什麼時候迭代過一個int對象?
出於好奇,你爲什麼認爲'i,j = 1'要將兩個變量賦值爲1? –
你有錯誤信息中有錯誤:'我,j = 1' – MartinHaTh