How do I add two sets

社会主义新天地 提交于 2020-03-13 05:34:05

问题


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'}

回答1:


All you have to do to combine them is 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.




回答2:


You can use update() to combine set(b) into set(a) Try to this.

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


来源:https://stackoverflow.com/questions/29648520/how-do-i-add-two-sets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!