如何修复“元组”对象不支持Python中的项目分配问题

时间:2019-02-16 04:12:23

标签: python sorting

我正在从头开始构建合并排序器,因为我对它的工作方式很感兴趣,但是我遇到了一个元组问题。这是什么意思,我该如何解决?

我最近开始通过基本教程学习Python 3并解决简单问题。

def MergeSort(alist):
    if len(alist)>1:
        mid = len(alist)//2
        leftHalf = alist[:mid]
        rightHalf = alist[mid:]

        MergeSort(leftHalf)
        MergeSort(rightHalf)
        i = 0
        j = 0
        k = 0
        while i<len(leftHalf) and j<len(rightHalf):
            if(leftHalf[i] < rightHalf[j]):
                alist[k] = rightHalf[i]
                i+= 1
            else:
                alist[k] = rightHalf[j]
                j += 1 
            k += 1

        while i<len(leftHalf):
            alist[k] = leftHalf[i]
            i += 1 
            k += 1 

        while j<len(rightHalf):
            alist[k] = rightHalf[j]
            j+= 1 
            k+= 1 
alist = (343, 543653, 643, -543, 534, -543234, 5432, 1232)
MergeSort(alist)
print(alist)

1 个答案:

答案 0 :(得分:0)

在Python中,字符串是不可变的。您应该将aList列为列表,以允许进行突变:

alist = [343, 543653, 643, -543, 534, -543234, 5432, 1232]