Python 集是可变的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14193438/
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
Are Python sets mutable?
提问by Hubro
Are sets in Python mutable?
Python 中的集合是可变的吗?
In other words, if I do this:
换句话说,如果我这样做:
x = set([1, 2, 3])
y = x
y |= set([4, 5, 6])
Are xand ystill pointing to the same object, or was a new set created and assigned to y?
是x和y仍指向同一对象,或者是一组新创建并分配给y?
采纳答案by Tom van der Woerdt
>>>> x = set([1, 2, 3])
>>>> y = x
>>>>
>>>> y |= set([4, 5, 6])
>>>> print x
set([1, 2, 3, 4, 5, 6])
>>>> print y
set([1, 2, 3, 4, 5, 6])
- Sets are unordered.
- Set elements are unique. Duplicate elements are not allowed.
- A set itself may be modified, but the elements contained in the set must be of an immutable type.
- 集合是无序的。
- 集合元素是唯一的。不允许重复元素。
- 集合本身可以被修改,但集合中包含的元素必须是不可变类型。
set1 = {1,2,3}
set2 = {1,2,[1,2]} --> unhashable type: 'list'
# Set elements should be immutable.
Conclusion: sets are mutable.
结论:集合是可变的。
回答by eumiro
print x,y
and you see they both point to the same set:
你会看到它们都指向同一个集合:
set([1, 2, 3, 4, 5, 6]) set([1, 2, 3, 4, 5, 6])
回答by Katriel
Your two questions are different.
你的两个问题是不同的。
Are Python sets mutable?
Python 集是可变的吗?
Yes: "mutable" means that you can change the object. For example, integers are not mutable: you cannot change the number 1to mean anything else. You can, however, add elements to a set, which mutates it.
是的:“可变”意味着您可以更改对象。例如,整数是不可变的:您不能将数字更改为1其他任何含义。但是,您可以将元素添加到集合中,从而对其进行变异。
Does
y = x; y |= {1,2,3}changex?
有
y = x; y |= {1,2,3}变化x吗?
Yes. The code y = xmeans "bind the name yto mean the same object that the name xcurrently represents". The code y |= {1,2,3}calls the magic method y.__ior__({1,2,3})under the hood, which mutates the object represented by the name y. Since this is the same object as is represented by x, you should expect the set to change.
是的。代码的y = x意思是“将名称绑定y到名称x当前代表的同一对象”。代码y |= {1,2,3}调用了底层的魔法方法y.__ior__({1,2,3}),它改变了 name 所代表的对象y。由于这与 表示的对象相同x,因此您应该期望该集合发生变化。
You can check whether two names point to precisely the same object using the isoperator: x is yjust if the objects represented by the names xand yare the same object.
您可以检查两个名字是否指向准确地使用相同的对象is操作:x is y只是如果对象表示由名称x和y是同一对象。
If you want to copy an object, the usual syntax is y = x.copy()or y = set(x). This is only a shallowcopy, however: although it copies the set object, the membersof said object are not copied. If you want a deepcopy, use copy.deepcopy(x).
如果要复制对象,通常的语法是y = x.copy()or y = set(x)。然而,这只是一个浅拷贝:虽然它复制了集合对象,但并未复制所述对象的成员。如果你想要一个深拷贝,使用copy.deepcopy(x).
回答by pawandeep singh
回答by Ashfaq Ur Rahman N
After changing the set, even their object references match. I don't know why that textbook says sets are immutable.
更改集合后,甚至它们的对象引用也匹配。我不知道为什么那本教科书说集合是不可变的。
>>> s1 ={1,2,3}
>>> id(s1)
140061513171016
>>> s1|={5,6,7}
>>> s1
{1, 2, 3, 5, 6, 7}
>>> id(s1)
140061513171016
回答by vivek
Python sets are classified into two types. Mutable and immutable. A set created with 'set' is mutable while the one created with 'frozenset' is immutable.
Python 集分为两种类型。可变的和不可变的。使用 'set' 创建的集合是可变的,而使用 'frozenset' 创建的集合是不可变的。
>>> s = set(list('hello'))
>>> type(s)
<class 'set'>
The following methods are for mutable sets.
以下方法适用于可变集。
s.add(item) -- Adds item to s. Has no effect if listis already in s.
s.add(item) -- 将项目添加到 s。如果list已经在 s 中则无效。
s.clear() -- Removes all items from s.
s.clear() -- 从 s 中删除所有项目。
s.difference_update(t) -- Removes all the items from s that are also in t.
s.difference_update(t) -- 从 s 中删除也在 t 中的所有项目。
s.discard(item) -- Removes item from s. If item is not a member of s, nothing happens.
s.discard(item) -- 从 s 中删除项目。如果 item 不是 s 的成员,则什么都不会发生。
All these operations modify the set s in place. The parameter t can be any object that supports iteration.
所有这些操作都会在适当的位置修改 set 。参数 t 可以是任何支持迭代的对象。
回答by Shashank Sahay
Sets are muttable
集合是可变的
s = {2,3,4,5,6}
type(s)
<class 'set'>
s.add(9)
s
{2, 3, 4, 5, 6, 9}
We are able to change elements of set
我们能够改变集合的元素
回答by Prabhjyot Singh
Yes, Python sets are mutable because we can add, delete elements into set, but sets can't contain mutable items into itself. Like the below code will give an error:
是的,Python 集合是可变的,因为我们可以在集合中添加、删除元素,但集合本身不能包含可变项。像下面的代码会报错:
s = set([[1,2,3],[4,5,6]])
So sets are mutable but can't contain mutable items, because set internally uses hashtable to store its elements so for that set elements need to be hashable. But mutable elements like list are not hashable.
所以集合是可变的,但不能包含可变项,因为集合内部使用哈希表来存储其元素,因此集合元素需要是可哈希的。但是像 list 这样的可变元素是不可散列的。
Note:
Mutableelements are not hashable
Immutableelements are hashable
注意:
可变元素不可散列
不可变元素可散列
Just like key of a dictionary can't be a list.
就像字典的键不能是列表一样。


