本文介绍了可变的变化而不会被触及的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python很陌生,但这很奇怪.因此,我有一个矩阵(一个列表列表),并将其传递给一个函数(creaMatrixVNS).

def creaMatrixVNS(best_matrice, time):
    matrice = best_matrice
    sol_init_ass = assegnamento(matrice)

    iteration = 1 #numero swap
    counter = 0 #numero iterazioni

    while True:
            temp = matrice

从这一点开始

            for x in xrange(0,iteration):
                    temp = swap(temp, sol_init_ass)

至此,我看到我的变量matrice发生了变化并将其设置为temp的值.而且我不知道为什么.那是函数的第二部分.

            time2 = creaSoluzione(temp)

            if time2 < time:
                    time = time2
                    matrice = temp
                    if counter == 9:
                            break
                    else:
                            counter += 1
            else:
                    if iteration < 3:
                            iteration += 1
                            counter = 0
                    else:
                            break

    print(time)
    return matrice

其他功能:

def swap(matriceInput, lista):
        result = matriceInput

        i1, j1 = giveMeAcouple(lista)
        i2, j2 = giveMeAcouple(lista)

        while i1 == i2 and j1 == j2:
                i1, j1 = giveMeAcouple(lista)
                i2, j2 = giveMeAcouple(lista)

        result[i1][j1], result[i2][j2] = result[i2][j2], result[i1][j1]

        return result

def giveMeAcouple(sol_init_ass):
        j1 = randint(0,len(sol_init_ass)-1)
        while len(sol_init_ass[j1]) <= 0:
                j1 = randint(0,len(sol_init_ass)-1)

        i1 = randint(0,len(sol_init_ass[j1]) - 1)

        return sol_init_ass[j1][i1], j1


def assegnamento(matrice):
        listElementi = []
        temp = []
        n = len(matrice)
        m = len(matrice[0])

        for i in xrange(0,m): #Per ogni colonna
                for j in xrange(0,n): #Per ogni riga
                        if matrice[j][i] != 0:
                                temp.append(j)
                listElementi.append(temp)
                temp = []

        return listElementi


def creaSoluzione(matrix):
        n = len(matrix)
        m = len(matrix[0])

        max = 0
        for j in xrange(0,m):
                temp = 0
                for i in xrange(0,n):
                        temp += matrix[i][j]
                #print(temp)

                if temp > max:
                        max = temp
        return max

有任何提示吗?谢谢

解决方案

执行matrice = best_matrice时,您只是在创建一个名为matrice的新引用,该引用指向与引用best_matrice相同的对象. >

在Python中深度复制列表,查看接受的答案Python中列表的深层副本的示例.

I'm quite new to Python but this is strange.So, I have a matrix (a list of lists) and I pass it to a function (creaMatrixVNS).

def creaMatrixVNS(best_matrice, time):
    matrice = best_matrice
    sol_init_ass = assegnamento(matrice)

    iteration = 1 #numero swap
    counter = 0 #numero iterazioni

    while True:
            temp = matrice

From this point

            for x in xrange(0,iteration):
                    temp = swap(temp, sol_init_ass)

to this point, I see that my variable matrice changes and is set to the value of temp. And I don't know why. That's the second part of the function.

            time2 = creaSoluzione(temp)

            if time2 < time:
                    time = time2
                    matrice = temp
                    if counter == 9:
                            break
                    else:
                            counter += 1
            else:
                    if iteration < 3:
                            iteration += 1
                            counter = 0
                    else:
                            break

    print(time)
    return matrice

Other functions:

def swap(matriceInput, lista):
        result = matriceInput

        i1, j1 = giveMeAcouple(lista)
        i2, j2 = giveMeAcouple(lista)

        while i1 == i2 and j1 == j2:
                i1, j1 = giveMeAcouple(lista)
                i2, j2 = giveMeAcouple(lista)

        result[i1][j1], result[i2][j2] = result[i2][j2], result[i1][j1]

        return result

def giveMeAcouple(sol_init_ass):
        j1 = randint(0,len(sol_init_ass)-1)
        while len(sol_init_ass[j1]) <= 0:
                j1 = randint(0,len(sol_init_ass)-1)

        i1 = randint(0,len(sol_init_ass[j1]) - 1)

        return sol_init_ass[j1][i1], j1


def assegnamento(matrice):
        listElementi = []
        temp = []
        n = len(matrice)
        m = len(matrice[0])

        for i in xrange(0,m): #Per ogni colonna
                for j in xrange(0,n): #Per ogni riga
                        if matrice[j][i] != 0:
                                temp.append(j)
                listElementi.append(temp)
                temp = []

        return listElementi


def creaSoluzione(matrix):
        n = len(matrix)
        m = len(matrix[0])

        max = 0
        for j in xrange(0,m):
                temp = 0
                for i in xrange(0,n):
                        temp += matrix[i][j]
                #print(temp)

                if temp > max:
                        max = temp
        return max

Any hints? Thanks

解决方案

When you do matrice = best_matrice, you're just creating a new reference named matrice pointing to the same object as the reference best_matrice.

Check out the accepted answer for Deep copy a list in Python for an example of a deep copy of a list in Python.

这篇关于可变的变化而不会被触及的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:36