列表之深淺拷貝
集合(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)
列表之深淺拷貝