python 集合set

时间:2021-08-11 11:17:22

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

下面来点简单的小例子说明把。  

>>> x = set('spam')
>>> y = set(['h','a','m'])
>>> x, y
(set(['a', 'p', 's', 'm']), set(['a', 'h', 'm'])) 再来些小应用。 >>> x & y # 交集
set(['a', 'm']) >>> x | y # 并集
set(['a', 'p', 's', 'h', 'm']) >>> x - y # 差集
set(['p', 's'])
a = t | s          # t 和 s的并集  

b = t & s          # t 和 s的交集  

c = t – s          # 求差集(项在t中,但不在s中)  

d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)  
#Python中,使用相应的优化函数可以大大的提高系统的运行效率。比如下面的这个例子:
from time import time
lista=[1,2, 3, 4,5, 6, 7 ,8, 9, 10, 25, 50, 36, 43, 52]
listb=[2, 4, 6, 9, 36]
def noset_test():
t = time()
filter=[]
for i in range(100000):
for a in lista:
for b in listb:
if a==b:
filter.append(a)
print 'no set total run time:'
print time() -t def set_test():
t1 = time()
for i in range(100000):
list(set(lista)&set(listb)) print "set total run time:"
print time() - t1 noset_test();
set_test(); 输出为:
no set total run time:
0.365000009537
set total run time:
0.15700006485 通过使用set,运行的时间明显的有缩减。
engineers = set(['John', 'Jane', 'Jack', 'Janice'])
programmers = set(['Jack', 'Sam', 'Susan', 'Janice'])
managers = set(['Jane', 'Jack', 'Susan', 'Zack'])
employees = list(engineers | programmers | managers) # union
engineering_management = list(engineers & managers) # intersection
fulltime_management = list(managers - engineers - programmers) # difference
print"employees", employees
print"engineering_management" ,engineering_management
print "fulltime_management", fulltime_management 输出为:
employees ['Jack', 'Sam', 'Susan', 'Jane', 'Janice', 'John', 'Zack']
engineering_management ['Jane', 'Jack']
fulltime_management ['Zack']