a = {'a','b','c'}
b = {'d','e','f'}
我想在上面添加两个设定值。
需要输出,
c = {'a','b','c','d','e','f'}
答案 0 :(得分:31)
要合并它们,您需要做的就是c = a|b
。
集是唯一值的无序序列。 a|b
是两个集合中的union
(一个新集合,其中包含在任一集合中找到的所有值)。这是一类称为“set operation”的操作,它为。提供了方便的工具。
答案 1 :(得分:17)
你可以使用update()将set(b)组合成set(a) 试试这个。
a = {'a', 'b', 'c'}
b = {'d', 'e', 'f'}
a.update(b)
print a
,第二个解决方案是:
c = a.copy()
c.update(b)
print c
答案 2 :(得分:2)
您可以在c中使用a和b的union()结果。注意:sorted()用于打印排序的输出
a = {'a','b','c'}
b = {'d','e','f'}
c=a.union(b)
print(sorted(c))
或仅打印a和b的排序后的并集
a = {'a','b','c'}
b = {'d','e','f'}
print(sorted(a.union(b)))