原文链接:http://www.juzicode.com/archives/2720
错误提示:
#juzicode.com/vx:桔子code
s1={1,2,3,4}#集合s1
s2={3,4,5,6}#集合s2
print('s1-s2:',s1-s2)
print('s1+s2:',s1+s2)
s1-s2: {1, 2}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-cb26c22afce4> in <module>
3 s2={3,4,5,6}#集合s2
4 print('s1-s2:',s1-s2)
----> 5 print('s1+s2:',s1+s2)
TypeError: unsupported operand type(s) for +: 'set' and 'set'
可能原因:
1、计算set的并集不支持使用+
解决方法:
1、使用union方法计算并集:
#juzicode.com/vx:桔子code
s1={1,2,3,4}#集合s1
s2={3,4,5,6}#集合s2
print('s1-s2:',s1-s2)
#print('s1+s2:',s1+s2)
su = s1.union(s2)
print('s1+s2:',su)
s1-s2: {1, 2}
s1+s2: {1, 2, 3, 4, 5, 6}
关注微信公众号:“桔子code”,欢迎后台留言撩我,我会尽我所能为你解惑Python,C等编程知识