python集合的difference方法返回两个集合的差集,差集中的元素存在于第一个集合而不在第二个集合中。
set.difference(*set)
集合的差集
code_language_1 = {'java', 'c++'}
code_language_2 = {'java', 'python'}
diff = code_language_1.difference(code_language_2)
print(diff)
diff是code_language_1与code_language_2的差集,如果要计算code_language_2与code_language_1差集,则应写成
code_language_2.difference(code_language_1)
接下来给出一个difference方法传入多个集合的例子
code_language_1 = {'java', 'c++', 'node.js'}
code_language_2 = {'java', 'python'}
code_language_3 = {'c++', 'python'}
diff = code_language_1.difference(code_language_2, code_language_3)
print(diff)
将code_language_2和code_language_3视为一个整体,code_language_1与他们进行差集计算
QQ交流群: 211426309