Wrong list being returned in python

后端 未结 1 1903
悲&欢浪女
悲&欢浪女 2021-01-25 01:59
def mainCall(nodeGroup):

    maxScore = -9999999
    maxPart = []
    tempPartition = []
    tempScore = 0.0

    for j in range(2, 1+nodes/2):

        nodeGroup = cho         


        
相关标签:
1条回答
  • 2021-01-25 02:47

    When you say

    maxPart = tempPartition
    

    you are not creating a copy of tempPartition but you are making the maxPart also to point to the same list that tempPartition points to. To really make a copy, you can use slicing notation like this

    maxPart[:] = tempPartition
    

    or

    maxPart = tempPartition[:]
    

    The difference between maxPart[:] = tempPartition and maxPart = tempPartition[:] is that the former mutates the maxPart and copies all the values from tempPartition to maxPart and latter creates a new list with a copy of all the data in tempPartition and makes the maxPart points to the newly created list.

    0 讨论(0)
提交回复
热议问题