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

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

How do I add two sets

pythonpython-3.xset

提问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|bis the unionof 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|bunion两个集合(一个新集合,其中包含在任一集合中找到的所有值)。这是一类称为“集合操作”的操作,它为集合提供了方便的工具。

回答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