简体   繁体   English

Python:更改列表列表中的元素

[英]Python: Change an element in a list of lists

I have 2 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]]
    ]

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: 当我更改'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]]

Integers are immutable in python. 整数在python中是不可变的。 One approach you can take is to enclose the integers in a container type (list) and modify the container values. 您可以采用的一种方法是将整数括在容器类型(列表)中并修改容器值。

See these additional pointers for related details: 有关详细信息,请参阅这些附加指针:

Python passing an integer by reference Python通过引用传递整数

How do I pass a variable by reference? 如何通过引用传递变量?

the issue here as other explained is that int objects are immutable So doing 这里的问题正如其他解释的那样,int对象是不可变的

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) 不会改变旧的两个[0] [0]对象,而是会创建新对象,并使两个[0] [0]引用这个新对象(旧对象是垃圾收集,如果没有其他参考它在这种情况下有)

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 我创建了一个非常简单的类,它保存int并使数组从中解决,这解决了你的问题

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

You're actually copying the values. 你实际上正在复制这些值。

Copy the reference to the lists in one: 将引用复制到一个列表中:

two=[one[0], one[1], one[2], one[3]]

Then you get for two[0][0]=9 : 然后你得到two[0][0]=9

one == [[9, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
two == [[9, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]

Is this what you're trying to accomplish? 这是你想要完成的吗?

def listfix(one, two):
    if one != two:
        one = two
    return one, two

two[0][0]=9
one, two = listfix(one, two)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM