Why can editing a copied label list change the source sample?

A shallow copy creates a new outer container. A deep copy recursively copies the contained objects that support copying.

Python example and interpretation

Two reviewers need independent mutable label lists. Copying an entire large object graph can be expensive; copying the edited field may suffice.

from copy import deepcopy
a={'tags':['old']}
b=deepcopy(a)
b['tags'].append('new')
[a,b]

Result [{'tags': ['old']}, {'tags': ['old', 'new']}]

中文

为何复制标注记录后修改标签列表会波及原样本?

浅复制只建立新的外层容器,深复制会递归复制其中可复制的对象。

Python 算例与适用条件

把样本复制给两位评审,需确保可变的标签列表彼此独立。深复制整张大型图成本较高,也可以只复制要修改的字段。

from copy import deepcopy
a={'tags':['old']}
b=deepcopy(a)
b['tags'].append('new')
[a,b]

结果 [{'tags': ['old']}, {'tags': ['old', 'new']}]

References / 参考来源

Keep reading / 继续阅读