将数据附加到多个元素的列表会将其附加到所有元素列表

时间:2017-07-28 14:19:40

标签: python list

给出以下示例代码:

class Parent:
    some_list = []  # type: list[int]

    def __init__(self, id):
        self.id = id

if __name__ == '__main__':
    list_of_parents = []  # type: list[Parent]

    # Create 10 parents
    for i in range(10):
        list_of_parents.append(Parent(i))

    # Go over each parent and add 10 integers to each of their lists
    for i, p in enumerate(list_of_parents):
        for j in range(10):
            list_of_parents[i].some_list.append(1)

执行该代码后,将会有一个包含10个Parent个对象的列表,每个对象都有一个包含100个条目的列表。 (包含10次整数0 - 9),即使这些j元素仅明确添加到list_of_parents[i]

原因是什么?

1 个答案:

答案 0 :(得分:1)

这就是Moses Koledoye已经提到过的。尝试这样的事情,你会看到差异:

class Parent:

    def __init__(self, id):
        self.id = id
        self.some_list = []# type: list[int]

if __name__ == '__main__':
    list_of_parents = []  # type: list[Parent]

    # Create 10 parents
    for i in range(10):
        list_of_parents.append(Parent(i))

    # Go over each parent and add 10 integers to each of their lists
    for i, p in enumerate(list_of_parents):
        for j in range(10):
            list_of_parents[i].some_list.append(1)
            print(list_of_parents[i].some_list)
            print("__________")

输出:

[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________