我有2个清单:
one = [ [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7] ] two = [ [one[0][0], one[0][1], one[1][0], one[1][1]], [one[0][2], one[0][3], one[1][2], one[1][3]], [one[2][0], one[2][1], one[3][0], one[3][1]], [one[2][2], one[2][3], one[3][2], one[3][3]] ]当我输入这个:
two[0][0] = 9它出来像这样:
one: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]] two: [[9, 2, 2, 3], [3, 4, 4, 5], [3, 4, 4, 5], [5, 6, 6, 7]]当我更改'two'列表中的元素时,如何获得与'one'列表中'two'列表相同的更改,以便:
one: [[9, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]] two: [[9, 2, 2, 3], [3, 4, 4, 5], [3, 4, 4, 5], [5, 6, 6, 7]]I have 2 lists:
one = [ [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7] ] two = [ [one[0][0], one[0][1], one[1][0], one[1][1]], [one[0][2], one[0][3], one[1][2], one[1][3]], [one[2][0], one[2][1], one[3][0], one[3][1]], [one[2][2], one[2][3], one[3][2], one[3][3]] ]When I input this one:
two[0][0] = 9It comes out like this:
one: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]] two: [[9, 2, 2, 3], [3, 4, 4, 5], [3, 4, 4, 5], [5, 6, 6, 7]]How can I get the same changes as in 'two' list in 'one' list when I changes the element in 'two' list, so that:
one: [[9, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]] two: [[9, 2, 2, 3], [3, 4, 4, 5], [3, 4, 4, 5], [5, 6, 6, 7]]最满意答案
这里的问题正如其他解释的那样,int对象是不可变的
two[0][0] = 9不会改变旧的两个[0] [0]对象,而是会创建新对象,并使两个[0] [0]引用这个新对象(旧对象是垃圾收集,如果没有其他参考它在这种情况下有)
并且在创建两个数组之后在这种情况下更清楚我们有以下( - >表示引用)
one[0][0] --> object(1) two[0][0] --> object(1)然后当你出手
two[0][0] = 9对象引用看起来像这样
one[0][0] --> object(1) two[0][0] --> object(9)因此,为了解决这个问题,我们需要将数组中的值更改为可变
我创建了一个非常简单的类,它保存int并使数组从中解决,这解决了你的问题
这是工作代码
class IntHolder: def __init__(self,int_value): self.intvalue = int_value def set_value(self,int_value): self.intvalue = int_value def __str__(self): return str(self.intvalue) def __repr__(self): return str(self.intvalue) def __str__(self): return str(self.intvalue) one = [ [IntHolder(1), IntHolder(2), IntHolder(3), IntHolder(4)], [IntHolder(2), IntHolder(3), IntHolder(4), IntHolder(5)], [IntHolder(3), IntHolder(4), IntHolder(5), IntHolder(6)], [IntHolder(4), IntHolder(5), IntHolder(6), IntHolder(7)] ] two = [ [one[0][0], one[0][1], one[1][0], one[1][1]], [one[0][2], one[0][3], one[1][2], one[1][3]], [one[2][0], one[2][1], one[3][0], one[3][1]], [one[2][2], one[2][3], one[3][2], one[3][3]] ] two[0][0].set_value(9) print one print twothe issue here as other explained is that int objects are immutable So doing
two[0][0] = 9wont change the old two[0][0] object instead it will create new object and make two[0][0] reference this new object (the old object is garbage collected if not other reference it in this case there is)
and to be more clear in this case after creating both arrays we have the following (--> means reference)
one[0][0] --> object(1) two[0][0] --> object(1)then when you excuted
two[0][0] = 9the object reference looks like this
one[0][0] --> object(1) two[0][0] --> object(9)So to solve this we need to change the values that you have in the array to be mutable
I have created very simple class that hold the int and made the array out of it and that resolves your issue
here is working code
class IntHolder: def __init__(self,int_value): self.intvalue = int_value def set_value(self,int_value): self.intvalue = int_value def __str__(self): return str(self.intvalue) def __repr__(self): return str(self.intvalue) def __str__(self): return str(self.intvalue) one = [ [IntHolder(1), IntHolder(2), IntHolder(3), IntHolder(4)], [IntHolder(2), IntHolder(3), IntHolder(4), IntHolder(5)], [IntHolder(3), IntHolder(4), IntHolder(5), IntHolder(6)], [IntHolder(4), IntHolder(5), IntHolder(6), IntHolder(7)] ] two = [ [one[0][0], one[0][1], one[1][0], one[1][1]], [one[0][2], one[0][3], one[1][2], one[1][3]], [one[2][0], one[2][1], one[3][0], one[3][1]], [one[2][2], one[2][3], one[3][2], one[3][3]] ] two[0][0].set_value(9) print one print two更多推荐
发布评论