您的位置 首页 > 腾讯云社区

Python中&、^与and、or---GhostCN_Z

导火索:给定两个列表,怎么找出他们相同的元素和不通的元素?list1 = [1, 2, 3, 4, 57, 8, 90] list2 = [2, 3, 4, 5, 6, 7, 8] lis = list1 + list2 print('&的结果:', set(list1) & set(list2)) print('^的结果:', set(list1) ^ set(list2)) print('and的结果:', set(list1) and set(list2)) print('or的结果:', set(list1) or set(list2))结果:&的结果: {8, 2, 3, 4} ^的结果: {1, 5, 6, 7, 57, 90} and的结果: {2, 3, 4, 5, 6, 7, 8} or的结果: {1, 2, 3, 4, 8, 57, 90}总结:

  & 不等于and,|不等于or

  &、^代表的是位运算符,andor代表的是逻辑运算符

&的源码:

def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value. """ pass ---来自腾讯云社区的---GhostCN_Z

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: