在python中返回了错误的列表 [英] Wrong list being returned in python

查看:82
本文介绍了在python中返回了错误的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def mainCall(nodeGroup):

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

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

        nodeGroup = chooseInitPartition(j, nodeGroup)
        tempScore, tempPartition = runKL(edgeList, nodeGroup, rounds)
        print 'temp score', tempScore
        print 'temp part', tempPartition, "\n"
        if(maxScore < tempScore):
            maxScore = tempScore
            maxPart = tempPartition
            print "max score", maxScore
            print "maxpart", maxPart, "\n"

     print 'before ret max part', maxPart
     return maxScore, maxPart

finalScore, finalPartition = mainCall(nodeGroup)

我上面的代码有问题.一切似乎都很好,直到for循环结束为止,但是此后,它打印了tempPartition的最后一个值(均代表数字列表),而不是在行print 'before ret max part'上打印maxPart值. 打印语句确认每次都不满足if条件,尤其是循环中的最后几次运行.所以我看不到tempPartition的值如何传输到maxPart.请帮我解决一下这个.这让我发疯.我确信我缺少一些简单的东西.谢谢!

I am having a problem with the above code. Everything seems fine till the for loop ends, but after that instead of printing the maxPart value at the line print 'before ret max part', it prints the last value of tempPartition (both represent a list of numbers). The print statements confirm that the if condition is not satisfied every time, especially the last few runs in the loop. So I don't see how the value of tempPartition is being transferred to maxPart. Please help me with this. It is driving me crazy. I am sure I am missing something simple. Thanks!

runKL

def runKL(edgeList, nodeGroup, rounds):

    nodeSwap = [0]*nodes
    maxLogLScore = 0.0
    edgeListLen = len(edgeList)

    networkPartitionStore = []
    logLScores = []

    #Reset count
    count = 0

    #Start main loop
    for i in range(rounds):
        #mark all vertices as unswapped
        for j in range(len(nodeSwap)):
            nodeSwap[j] = 0


        while(count < 100):
            #Choose one edge uniformly randomly
            randNum = random.uniform(0,1)
            edge = edgeList[int(math.floor(edgeListLen*randNum))]
            node1 = int(edge[0]) - 1
            node2 = int(edge[1]) - 1

            if((nodeGroup[node1] == nodeGroup[node2]) or (nodeSwap[node1] == 1) or (nodeSwap[node2] == 1)):
                count += 1
                continue
            else:
                #swap groups among nodes
                temp = nodeGroup[node1]
                nodeGroup[node1] = nodeGroup[node2]
                nodeGroup[node2] = temp

                #mark vertices as swapped
                nodeSwap[node1] = 1
                nodeSwap[node2] = 1

                #calculate likelihood
                logLScore = logLikelihood(edgeList, nodeGroup)
                logLScores.append(logLScore)

                #store network
                networkPartitionStore.append(nodeGroup)

                #reset count value
                count = 0

        #end while loop

        #Choose the index of the maximum likelihood score
        maxLogLScore = max(logLScores)
        index = logLScores.index(maxLogLScore)
        #print 'max score', modularityScores[index]

        #Choose the corresponding network partition i.e. the way the groups have been assigned to the nodes
        nodeGroup = networkPartitionStore[index]

        #Reset partition storage list and modularity scores
        networkPartitionStore = []
        logLScores = []

        #Store initial network partition structure and modularity score. 
        networkPartitionStore.append(nodeGroup)
        logLScores.append(maxLogLScore)

    return maxLogLScore, nodeGroup

推荐答案

当你说

maxPart = tempPartition

您不是要创建tempPartition的副本,而是要使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

maxPart = tempPartition[:]

maxPart[:] = tempPartitionmaxPart = tempPartition[:]之间的区别在于,前者会变异maxPart并将所有值从tempPartition复制到maxPart,而后者会创建一个新列表,其中包含tempPartition,并使maxPart指向新创建的列表.

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.

这篇关于在python中返回了错误的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆