One - One Code All

Blog Content

python集合的操作,并集,交集,差集

Python   2009-01-29 21:45:27

集合  
  
集合用于包含一组无序的对象。要创建集合,可使用set()函数并像下面这样提供一系列的项: 

s = set([3,5,9,10])      #创建一个数值集合 
t = set("Hello")         #创建一个唯一字符的集合 

与列表和元组不同,集合是无序的,也无法通过数字进行索引。此外,集合中的元素不能重复。
集合支持一系列标准操作,包括并集、交集、差集和对称差集,例如: 
 
a = t | s          # t 和 s的并集 
b = t & s          # t 和 s的交集 
c = t – s          # 求差集(项在t中,但不在s中) 
d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)


上一篇:python判断字符串是否为合法的json格式
下一篇:python风格PEP8报错:under-indented for visual indent

The minute you think of giving up, think of the reason why you held on so long.