Python 怎么加两套
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29648520/
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
How do I add two sets
提问by GThamizh
a = {'a','b','c'}
b = {'d','e','f'}
I want to add above two set values.
我想添加以上两个设定值。
Need output like,
需要输出,如
c = {'a','b','c','d','e','f'}
回答by TheBlackCat
All you have to do to combine them is c = a|b
.
要将它们组合起来,您所要做的就是c = a|b
.
Sets are unordered sequences of unique values. a|b
is the union
of the two sets (a new set with all values found in either set). This is a class of operation called a "set operation", which sets provide convenient tools for.
集合是唯一值的无序序列。 a|b
是union
两个集合(一个新集合,其中包含在任一集合中找到的所有值)。这是一类称为“集合操作”的操作,它为集合提供了方便的工具。
回答by Haresh Shyara
You can use update() to combine set(b) into set(a) Try to this.
您可以使用 update() 将 set(b) 合并为 set(a) 试试这个。
a = {'a', 'b', 'c'}
b = {'d', 'e', 'f'}
a.update(b)
print a
and second solution is:
第二个解决方案是:
c = a.copy()
c.update(b)
print c