如何在 Python 中克隆或复制一个集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23200969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:29:03  来源:igfitidea点击:

How to clone or copy a set in Python?

pythonsetcloneshallow-copy

提问by Evgeni Sergeev

For copying a list: shallow_copy_of_list = old_list[:].

用于复制列表:shallow_copy_of_list = old_list[:]

For copying a dict: shallow_copy_of_dict = dict(old_dict).

用于复制字典:shallow_copy_of_dict = dict(old_dict).

But for a set, I was worried that a similar thing wouldn't work, because saying new_set = set(old_set)would give a set of a set?

但是对于 a set,我担心类似的事情不起作用,因为 saynew_set = set(old_set)会给出一组一组?

But it does work. So I'm posting the question and answer here for reference. In case anyone else has the same confusion.

但它确实有效。所以我在这里发布问题和答案以供参考。万一其他人有同样的困惑。

回答by Evgeni Sergeev

Both of these will give a duplicate of a set:

这两个都会给出一个集合的副本:

shallow_copy_of_set = set(old_set)

Or:

或者:

shallow_copy_of_set = old_set.copy() #Which is more readable.

The reason that the first way above doesn'tgive a set of a set, is that the proper syntax for that would be set([old_set]). Which wouldn't work, because sets can't be elements in other sets, because they are unhashable by virtue of being mutable. However, this isn't true for frozensets, so e.g. frozenset(frozenset(frozenset([1,2,3]))) == frozenset([1, 2, 3]).

上面的第一种方法没有给出一组集合的原因是,正确的语法是set([old_set]). 这是行不通的,因为sets 不能是其他sets 中的元素,因为它们是可变的,因此不可散列。但是,这对于frozensets 而言并非如此,因此例如frozenset(frozenset(frozenset([1,2,3]))) == frozenset([1, 2, 3]).

So a rule of thumb for replicating any of instance of the basic data structures in Python (lists, dict, set, frozenset, string):

因此,在 Python 中复制任何基本数据结构实例的经验法则(列表、字典、集合、冻结集、字符串):

a2 = list(a)      #a is a list
b2 = set(b)       #b is a set
c2 = dict(c)      #c is a dict
d2 = frozenset(d) #d is a frozenset
e2 = str(e)       #e is a string
#All of the above give a (shallow) copy.

So, if xis either of those types, then

所以,如果x是这两种类型中的任何一种,那么

shallow_copy_of_x = type(x)(x) #Highly unreadable! But economical.

Note that only dict, setand frozensethave the built-in copy()method. It would probably be a good idea that lists and strings had a copy()method too, for uniformity and readability. But they don't, at least in Python 2.7.3 which I'm testing with.

请注意,只有dict,setfrozenset具有内置copy()方法。copy()为了一致性和可读性,列表和字符串也有一个方法可能是一个好主意。但他们没有,至少在我正在测试的 Python 2.7.3 中是这样。

回答by zhangxaochen

Besides the type(x)(x)hack, you can import copymodule to make either shallow copy or deep copy:

除了type(x)(x)hack 之外,您还可以导入copy模块以进行浅拷贝或深拷贝:

In [29]: d={1: [2,3]}

In [30]: sd=copy.copy(d)
    ...: sd[1][0]=321
    ...: print d
{1: [321, 3]}

In [31]: dd=copy.deepcopy(d)
    ...: dd[1][0]=987
    ...: print dd, d
{1: [987, 3]} {1: [321, 3]}

From the docstring:

从文档字符串:

Definition: copy.copy(x)
Docstring:
Shallow copy operation on arbitrary Python objects.