集合( set )

集合(set):把不同的元素组成一起形成集合,是python基本的数据类型
集合元素(set elements):组成集合的成员(不可重复)

集合是一个无序的,不重复的数据组合,它的主要作用如下:
     1.去重,把一个列表变成集合,就自动去重了
     2.关系测试,测试两组数据之前的交集、差集、并集等关系
                          
#可以去重
s = set("jayce chen")
print(s)#{'y', 'j', ' ', 'c', 'h', 'n', 'a', 'e'}
s1 = ['alvin','ee','alvin']
s2 = set(s1)
print(s2,type(s2))
s3 = list(s2)
print(s3,type(s3))


集合对象是一组无序排列的可哈希的值:集合成员可以做字典的键
li = [[1,2],3,'jayce']
s = set(li)
print(s)

    s = set(li)
TypeError: unhashable type: 'list'


li = [{1:'MM'},3,'jayce']
s = set(li)
print(s)

    s = set(li)
TypeError: unhashable type: 'dict'

li = [2,3,'jayce']
s = set(li)
print(s)#{2, 3, 'jayce'}

集合分类:可变集合、不可变集合
可变集合(set):可添加和删除元素,非可哈希的,不能用作字典的键,也不能做其他集合的元素
不可变集合(frozenset):与上面恰恰相反
li = [2,3,'jayce']
s = set(li)
print(s)
dic = {s:'137'}   # unhashable type: 'set'


1、创建集合
      由于集合没有自己的语法格式,只能通过集合的工厂方法set()和frozenset()创建
     s1 = set('alvin')
     s2= frozenset('yuan')
     print(s1,type(s1))  #{'l', 'v', 'i', 'a', 'n'} <class 'set'>
     print(s2,type(s2))  #frozenset({'n', 'y', 'a', 'u'}) <class 'frozenset'>

2、访问集合
由于集合本身是无序的,所以不能为集合创建索引或切片操作,只能循环遍历或使
用in、not in来访问或判断集合元素。
li = [2,3,'jayce']
s = set(li)
print(s)
print(2 in s)#True
print(4 in s)#False
print('jayce' in s)#True
print('jay' in s)#False

3、更新集合
可使用以下内建方法来更新:
s.add()
s.update()
s.remove()

注意只有可变集合才能更新:
li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.add('MM') #添加一个元素
print(s)   #{'jayce', 2, 3, 'MM'}


update(self, *args, **kwargs): # real signature unknown
         """ Update a set with the union of itself and others. """
li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.update("ops")
print(s)#{'p', 2, 3, 'o', 'jayce', 's'}

li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.update([12,'abc'])
print(s)#{2, 3, 'abc', 12, 'jayce'}

li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.update([12,'jayce'])
print(s)#{2, 3, 12, 'jayce'}


remove(self, *args, **kwargs): # real signature unknown
         """
         Remove an element from a set; it must be a member.
        
         If the element is not a member, raise a KeyError.
         """
li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.remove(2)
print(s)#{'jayce', 3}


pop(self, *args, **kwargs): # real signature unknown
         """
         Remove and return an arbitrary set element.
         Raises KeyError if the set is empty.
         """
随机删除
li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
s.pop()
print(s)

del
li = [2,3,'jayce']
s = set(li)
print(s)   #{'jayce', 2, 3}
del s
print(s)
报错:
     print(s)
NameError: name 's' is not defined

四、集合类型操作符 
1   in ,not in

2   集合等价与不等价(==, !=)
print( set('jayce') == set("jayceycecece"))#True

3   子集、超集
    a = set( [1,2,3,4,5] )
    b = set([4,5,6,7,8])
    print(a.issuperset(b))  #超集 a>b  False
    print(a.issubset(b))    #子集 a<b  False

4   并集(|)
     并集(union)操作与集合的or操作其实等价的,联合符号有个等价的方法,union()。

a = set( [1,2,3,4,5] )
b = set([4,5,6,7,8])   
print(a.union(b))#{1, 2, 3, 4, 5, 6, 7, 8}   
print(a | b)  #{1, 2, 3, 4, 5, 6, 7, 8}

print( set('jayce') and set("jaycemm"))  #{'y', 'e', 'a', 'j', 'c', 'm'}
print( set('jayce') or set("jaycemm")    #{'a', 'y', 'c', 'j', 'e'}

5、交集(&)
与集合and等价,交集符号的等价方法是intersection()
intersection(self, *args, **kwargs): # real signature unknown
         """
         Return the intersection of two sets as a new set.
        
         (i.e. all elements that are in both sets.)
         """

a = set( [1,2,3,4,5] )
b = set([4,5,6,7,8])
print(a.intersection(b))#{4, 5}
print(a & b)  #{4, 5}


6.差集
difference(self, *args, **kwargs): # real signature unknown
         """
         Return the difference of two or more sets as a new set.
        
         (i.e. all elements that are in this set but not the others.)
         """

a = set( [1,2,3,4,5] )
b = set([4,5,6,7,8])
print(a.difference(b)) # a - b {1, 2, 3}  a 里面有 b 里面没有
print(b.difference(a)) # b - a {8, 6, 7}  b 里面有 a 里面没有
print(a - b)#{1, 2, 3}
print(b - a)#{8, 6, 7}

7、对称差集(^)

对称差集是集合的XOR(‘异或’),取得的元素属于s1,s2但不同时属于s1和s2.
symmetric:对称 
symmetric_difference(self, *args, **kwargs): # real signature unknown
         """
         Return the symmetric difference of two sets as a new set.
        
         (i.e. all elements that are in exactly one of the sets.)
         """

a = set( [1,2,3,4,5] )
b = set([4,5,6,7,8])
print(a.symmetric_difference(b))#{1, 2, 3, 6, 7, 8}
print(a^b)

猜你喜欢

转载自www.cnblogs.com/lengyao888/p/10462629.html
今日推荐