###集合是无序的,不重复的元素集,类似数学中的集合,可进行逻辑运算和算术运算。 #无序 不重复 #set([list]) s=set(['a','b','c']) #集合对象s print "集合s的长度",len(s) print 'a' in s print 'd' not in s s1=set([1,2,3,4,5]) s2=set([1,2,3,4,5,6,7]) print "s1是否s2的子集:",s1.issubset(s2),"子集:等价于s1<=s2" #子集 print "s1是否s2的超集:",s1.issuperset(s2),"超集:等价于s1>=s2" #超集 s=set([1,2,3,4,5,6,7,8,9,10]) u=set([4,5,6,9,11,12,13,14,15]) print "s和u的并,等价于s|u:",s.union(u) #并集 print s|u print "s和u的交,等价于s&u:",s.intersection(u) #交集 print s&u print s.difference(u) #集合的差 即:s中有的并且u中没有的 print s-u print u-s print s.symmetric_difference(u) #集合的异或 即:s|u - s&u print s^u
#!/usr/bin/python # -*- coding: UTF-8 -*- #用集合删除重复项 distances={10.6,11,8,10.6,"two",7} print type(distances) #distance 为集合对象 print distances for x in distances: print x mylist=[10.6,11,10.6,17.6,17.6,17.6] myset=set(mylist) print mylist print myset