Python 索引值未更改为其应更改的值(第9行)

Python 索引值未更改为其应更改的值(第9行),python,Python,这是python3中的完整代码: def rainwater(array): max_value = max(array) count = 0 for a in range(0, max_value): temp = array for i in range(0, len(temp)): if temp[i] >= 1: print(temp) t

这是python3中的完整代码:

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))
def雨水(阵列):
最大值=最大值(数组)
计数=0
对于范围内的(0,最大值):
温度=阵列
对于范围(0,len(temp))内的i:
如果温度[i]>=1:
打印(临时)
temp[i]=1#不知怎的,这一行总是将temp[i]更改为0而不是1
数组[i]=数组[i]-temp[i]
阵列=温度
对于范围(0,len(temp))内的i:
如果温度[i]==1:
del temp[:i]
打破
对于范围(0,透镜(温度)-1,-1)内的i:
如果温度[i]==1:
德尔泰姆[i:]
打破
如果len(temp)返回6
打印(雨水([0,1,0,2,1,0,1,3,2,1,2,1]))

问题发生在第9行。我不知道该如何对这个问题进行分类,但我一辈子也弄不清楚这里缺少了什么。提前感谢。

问题

问题在于将temp指定为数组

使用
temp=array
,实际上没有两个列表。赋值只是将引用复制到列表,而不是实际列表,因此赋值后temp和array都引用同一个列表

temp = array
for i in range(0, len(temp)):
    if temp[i] >= 1:
        print(temp)
        temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
        array[i] = array[i] - temp[i]

    array = temp
在上述代码中,更改为
temp[i]
changes
array[i]
, 所以

因此,温度[i]是0而不是1

如何解决

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array[:]
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp[:]

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))
您可以使用
copy
deepcopy
slicing
将阵列复制到temp

下面是更新后的代码,使用
slicing

代码

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array[:]
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp[:]

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))
def雨水(阵列):
最大值=最大值(数组)
计数=0
对于范围内的(0,最大值):
温度=数组[:]
对于范围(0,len(temp))内的i:
如果温度[i]>=1:
打印(临时)
temp[i]=1#不知怎的,这一行总是将temp[i]更改为0而不是1
数组[i]=数组[i]-temp[i]
数组=温度[:]
对于范围(0,len(temp))内的i:
如果温度[i]==1:
del temp[:i]
打破
对于范围(0,透镜(温度)-1,-1)内的i:
如果温度[i]==1:
德尔泰姆[i:]
打破
如果len(temp)返回6
打印(雨水([0,1,0,2,1,0,1,3,2,1,2,1]))

问题

问题在于将temp指定为数组

使用
temp=array
,实际上没有两个列表。赋值只是将引用复制到列表,而不是实际列表,因此赋值后temp和array都引用同一个列表

temp = array
for i in range(0, len(temp)):
    if temp[i] >= 1:
        print(temp)
        temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
        array[i] = array[i] - temp[i]

    array = temp
在上述代码中,更改为
temp[i]
changes
array[i]
, 所以

因此,温度[i]是0而不是1

如何解决

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array[:]
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp[:]

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))
您可以使用
copy
deepcopy
slicing
将阵列复制到temp

下面是更新后的代码,使用
slicing

代码

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array[:]
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp[:]

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))
def雨水(阵列):
最大值=最大值(数组)
计数=0
对于范围内的(0,最大值):
温度=数组[:]
对于范围(0,len(temp))内的i:
如果温度[i]>=1:
打印(临时)
temp[i]=1#不知怎的,这一行总是将temp[i]更改为0而不是1
数组[i]=数组[i]-temp[i]
数组=温度[:]
对于范围(0,len(temp))内的i:
如果温度[i]==1:
del temp[:i]
打破
对于范围(0,透镜(温度)-1,-1)内的i:
如果温度[i]==1:
德尔泰姆[i:]
打破
如果len(temp)返回6
打印(雨水([0,1,0,2,1,0,1,3,2,1,2,1]))

此行将您的值设置为0:

array[i] = array[i] - temp[i]
原因是数组和temp是相同的对象。所以x-x总是为零

我认为您假设第5行的代码是数组的副本:

temp = array
但事实并非如此。这使得
temp
array
引用相同的数组。如果确实需要阵列的副本,请执行以下操作:

temp = array[:]
此外,如果要复制,您可能还需要修复第11行


我建议您阅读更多关于按引用和按值传递变量的内容。

此行将您的值设置为0:

array[i] = array[i] - temp[i]
原因是数组和temp是相同的对象。所以x-x总是为零

我认为您假设第5行的代码是数组的副本:

temp = array
但事实并非如此。这使得
temp
array
引用相同的数组。如果确实需要阵列的副本,请执行以下操作:

temp = array[:]
此外,如果要复制,您可能还需要修复第11行


我建议您阅读更多关于通过引用和值传递变量的内容。

请检查并接受答案请检查并接受答案