问题
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