我正在尝试在 Python 中实现快速排序的分区函数。
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])
.
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 对象呢?