python中的副本列表

时间:2011-08-09 08:46:09

标签: python list memory multidimensional-array

因为我正在尝试复制列表并使用列表副本执行某些操作。不知何故,我的原始列表也被修改了。我已经看过不同的内存分配和不同的分配方式。到目前为止没有运气......有什么想法吗?

    row = 0
    column = 0
    table1 = copy.copy(table[:])

    temptable = []
    temptable = table[:]

    print id(table)
    print table
    print id(table1)
    print table1
    print id(temptable)
    print temptable

    for i in temptable:
        for j in i:
            if type(j) == str:
                temptable[row][column] = 0
            column = column + 1
        column = 0
        row = row + 1
    result=[]   

    for column in zip(*temptable):
        try:
                result.append(sum(map(int,column)))
            except ValueError:
                result.append(0)


    print table
    print table1
    print temptable

/ #### Results

163783148
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

163669036
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

163783468
[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], ['BBB', 1, 1, 0, 26, 28, 0], ['CCC', 26, 0, 0, 0, 26, 0], ['DDD', 0, 26, 0, 0, 26, 0], ['EEE', 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

[[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 26, 28, 0], [0, 26, 0, 0, 0, 26, 0], [0, 0, 26, 0, 0, 26, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]

4 个答案:

答案 0 :(得分:12)

您的原始列表包含内部列表:

[[0, 'ZZZ', 'XXX', 'YYY', 'AAA', 0, 0], 
 ['BBB', 1, 1, 0, 26, 28, 0], ...
]

内部列表实际上存储为引用,即:

[ location-of-list-0, 
  location-of-list-1, ...
]

复制列表时,实际上复制了对原始列表中存储的相同列表的引用列表。这称为浅拷贝,因为它复制引用而不是内容。

使用deep copy创建一个完全独立的列表。

插图

原始列表

enter image description here

浅拷贝

enter image description here

深层复制

enter image description here

答案 1 :(得分:4)

您还需要deepcopy来复制列表中的对象,而不仅仅是参考文件。

答案 2 :(得分:2)

您还应该复制内部列表,因此deepcopy可以为您提供帮助。

答案 3 :(得分:0)

由于[:]仅创建实际应用切片的列表的副本切片,因此对对象的引用保持不变。 使用deepcopy会递归复制每个可以指出的项目。

class Foo:

    def __init__(self):
        self.name   = "unknown"

    def __repr__(self):
        return "Foo: " + self.name

    def __copy__(self):
        f = Foo()
        f.name = self.name
        return f

    def __deepcopy__(self, d):
        return self.__copy__()

lst = [Foo()]
lst[0].name = "My first foo"

lst2 = lst[:]
lst2.append(Foo())
lst2[1].name = "My second foo"

print lst
print lst2

  

输出

     

[Foo:我的第一个foo]
  [Foo:我的第一个foo,Foo:我的第二个foo]

lst2[0].name = "I changed the first foo"

print lst
print lst2

  

输出

     

[Foo:我改变了第一个foo]
  [Foo:我改变了第一个foo,Foo:我的第二个foo]

from copy import deepcopy
lst[0].name = "My first foo"
lst3 = deepcopy(lst)
lst3[0].name = "I changed the first foo again !"

print lst
print lst3

  

输出

     

[Foo:我改变了第一个foo]
  [Foo:我又改变了第一个foo!]

列表,元组等支持__copy____deepcopy__方法。