Python:更改列表列表中的元素(Python: Change an element in a list of lists)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
Python:更改列表列表中的元素(Python: Change an element in a list of lists)

我有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] = 9

It 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 two

the issue here as other explained is that int objects are immutable So doing

two[0][0] = 9

wont 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] = 9

the 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

更多推荐

本文发布于:2023-04-24 20:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c351d17f749a05446f7df6fdec32a8d7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   列表   列表中   Python   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!