python学习笔记整理——集合 set

python学习整理笔记--集合 set

集合的用途:成员测试和消除重复的条目,进行集合运算

  • 注意:花括号或set()函数可以用于创建集合。
  • 注意:若要创建一个空的集合你必须使用set(),不能用{};{}将创建一个空的字典

运算与用法

  1. 并集

a | b # letters in either a or b

  1. 交集

a & b # letters in both a and b

a - b # letters in a but not in b

  1. 对称

a ^ b # letters in a or b but not both

支持推导式

  >>> a = {x for x in 'abracadabra' if x not in 'abc'}
  >>> a
  set(['r', 'd'])
原文地址:https://www.cnblogs.com/learn-to-rock/p/python1003.html