【问题标题】:Copied old list reference being changed by new list object modification in python复制的旧列表引用被python中的新列表对象修改更改
【发布时间】:2015-07-30 14:15:11
【问题描述】:

我正在解决这个问题:http://codeforces.com/contest/540/problem/C

这似乎是在坐标平面上的标准广度优先搜索,所以本质上,您可以水平或垂直移动,每次移动时使用字符“X”更新新位置的单元格

为了解决它,我必须创建所有可能的相邻状态对象的列表,其中“状态”对象包含一个位置和一个二维数组(坐标平面/矩阵)。我在下面的“getAdjacentStates”类函数中创建了这个相邻“状态”对象的列表:

class State(object):

def __init__(self, matrix, curPosition):
    self.curMatrix = matrix
    self.curPosition = curPosition

def __hash__(self):
    hash = 0
    mult = 1
    for row in range(len(self.curMatrix)):
        for col in range(len(self.curMatrix[0])):
            if(self.curMatrix[row][col] == 'X'):
                hash += mult * 1
            mult*=2
    return hash

def __eq__(self, other):
    for i, row in enumerate(other.curMatrix):
        for j, col in enumerate(other.curMatrix[0]):
            if not(self.curMatrix[i][j] == other.curMatrix[i][j]):
                return False
    return True


#adjacentStates
def getAdjacentStates(self):
    #calculate a new matrix from curMatrix based on horizontal and vertical position movements
    #curMatrix & curPosition

    adjStates = list()

    if(self.curPosition.column > 0):
        # Subtract one from the column of the current position
        leftPos = Position(self.curPosition.row, self.curPosition.column - 1)
        adjStates.append(createState(self.curMatrix, leftPos))

    if(self.curPosition.column < len(self.curMatrix[0]) - 1):
        # Add one to the column of the current position
        rightPos = Position(self.curPosition.row, self.curPosition.column + 1)
        adjStates.append(createState(self.curMatrix, rightPos))

    if(self.curPosition.row > 0):
        # Subtract one from the current position's row
        downPos = Position(self.curPosition.row-1, self.curPosition.column)
        adjStates.append(createState(self.curMatrix, downPos))

    if(self.curPosition.row < len(self.curMatrix) - 1):
        # Add one to the row of the current position
        upPos = Position(self.curPosition.row + 1, self.curPosition.column)
        adjStates.append(createState(self.curMatrix, upPos))
    #print()
    return adjStates

带有附加功能:

def createState(stateMatrix, position):
    newMatrix = list()
    newMatrix = [stateMatrix[row] for row in range(len(stateMatrix))]
    newMatrix[position.row][position.column] = 'X'
    return State(newMatrix, position)

这一切都可以在正确的方向上导航,但是当我打印出具有给定初始状态的相邻状态列表时:

5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1

我得到了 3 个相同的:

.X..
...X
X.X.
..X.
.XXX

.X..
...X
X.X.
..X.
.XXX

.X..
...X
X.X.
..X.
.XXX

即使它看起来应该是这样的:

.X..
...X
X.X.
....
.XX.

.X..
...X
X.X.
....
.XXX

.X..
...X
X.X.
..X.
.XX.

不要介意走上“X”的缺陷

比较它应该是什么和我得到什么,由于某种原因,相邻的 State 对象都有指向一个公共“curMatrix”字段的指针。我不知道为什么,因为在我的“createState”函数中,我创建了一个完全独立的列表对象并返回一个新的 State 对象。

如果有人可以就如何解决这个问题提供一些建议,我将非常感谢 python 初学者的帮助,谢谢!

完整代码如下:

import queue

class Position(object):
    def __init__(self, row, column):
        self.row = row
        self.column = column


def createState(stateMatrix, position):
    newMatrix = list()
    newMatrix = [stateMatrix[row] for row in range(len(stateMatrix))]
    newMatrix[position.row][position.column] = 'X'
    return State(newMatrix, position)

class State(object):

    def __init__(self, matrix, curPosition):
        self.curMatrix = matrix
        self.curPosition = curPosition

    def __hash__(self):
        hash = 0
        mult = 1
        for row in range(len(self.matrix)):
            for col in range(len(self.matrix[0])):
                if(self.matrix[row][col] == 'X'):
                    hash += mult * 1
                mult*=2
        return hash

    def __eq__(self, other):
        for i, row in enumerate(other.curMatrix):
            for j, col in enumerate(other.curMatrix[0]):
                if not(self.curMatrix[i][j] == other.curMatrix[i][j]):
                    return False
        return True


    #adjacentStates
    def getAdjacentStates(self):
        #calculate a new matrix from curMatrix based on horizontal and vertical position movements
        #curMatrix & curPosition
        adjStates = list()
        if(self.curPosition.column > 0):
            # Subtract one from the column of the current position
            leftPos = Position(self.curPosition.row, self.curPosition.column - 1)
            adjStates.append(createState(self.curMatrix, leftPos))
        if(self.curPosition.column < len(self.curMatrix[0]) - 1):
            # Add one to the column of the current position
            rightPos = Position(self.curPosition.row, self.curPosition.column + 1)
            adjStates.append(createState(self.curMatrix, rightPos))

        if(self.curPosition.row > 0):
            # Subtract one from the current position's row
            downPos = Position(self.curPosition.row-1, self.curPosition.column)
            adjStates.append(createState(self.curMatrix, downPos))

        if(self.curPosition.row < len(self.curMatrix) - 1):
            # Add one to the row of the current position
            upPos = Position(self.curPosition.row + 1, self.curPosition.column)
            adjStates.append(createState(self.curMatrix, upPos))
        #print()
        return adjStates



    #isFinalState

    def __repr__(self):
        for i, row in enumerate(self.curMatrix):
            for j, col in enumerate(self.curMatrix[i]):
                print(self.curMatrix[i][j], end='')
            print()



n, m = list(map(int, input().split()))
startMatrix = [list(input()) for x in range(n)] #startMatrix[row][column]
startRow, startColumn = list(map(int, input().split()))
startPos = Position(startRow-1, startColumn-1)
endRow, endColumn = list(map(int, input().split()))
endPos = Position(endRow-1, endColumn-1)

initialState = createState(startMatrix, startPos)

testAdj = initialState.getAdjacentStates()
for adj in testAdj:
    adj.__repr__()
    print()

【问题讨论】:

    标签: python object multidimensional-array pass-by-reference pass-by-value


    【解决方案1】:

    实际上,你在adjStates.append(createState(self.curMatrix, xxxPos))中使用了相同的列表

    def createState(stateMatrix, position):
        newMatrix = list()
        newMatrix = [stateMatrix[row] for row in range(len(stateMatrix))]
        newMatrix[position.row][position.column] = 'X'
        return State(newMatrix, position)
    

    我想你需要原始列表的副本,所以将上面的代码更改为:

    def createState(stateMatrix, position):
        # it is superfluous code, so I've just commented it
        # newMatrix = list()
    
        # Here is the change - create copies of all source lists
        # newMatrix = [list(stateMatrix[row]) for row in range(len(stateMatrix))]
        # But there is a simpler way - iterate direct through list items:
        newMatrix = [list(row) for row in stateMatrix]
        newMatrix[position.row][position.column] = 'X'
        return State(newMatrix, position)
    

    【讨论】:

    • 老实说,我不知道为什么会这样,因为我最初做了同样的事情,我没有先调用 'newMatrix = list()' 而是调用了:'newMatrix = [stateMatrix[row ] for row in range(len(stateMatrix))]' 首先,在我看来,这与您更改的第一行相同:'newMatrix = [list(row) for row in stateMatrix]'。如果您有时间,请详细说明为什么“newMatrix = list()”使用相同的列表对象?还有,谢谢,谢谢,xinfinity
    • 关键是如何创建现有列表的副本。 stateMatrix = [a, b, c],newMatrix = [a, b, c]。 newMatrix 不是 stateMatrix。但是newMatrix[0] is stateMatrix[0],它是a。要创建列表a, b, c 的副本,您应该调用list(a), list(b), list(c)。简单的例子:a = [[1,2], [3,4]] 如果你创建了b = [row for row in a] 那么a[0] 和b[0] 是同一个对象,所以当你改变b[0] 的项目时,你也改变了a[0] 的项目。如果你创建b = [list(row) for row in a],那么b[0]和a[0]是不同的对象,所以b[0]上的改变不会影响a[0]。
    • 噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢!这就说得通了。我没有考虑变量是指针,也没有使用 list() 来构造一个完全独立的对象。感谢您的帮助和额外的课程! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多