您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Python中什么是set集合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
認識python中的set集合及其用法
python中,集合(set)是一個無序排列,可哈希,支持集合關系測試,不支持索引和切片操作,沒有特定語法格式,只能通過工廠函數創建.集合里不會出現兩個相同的元素,所以集合常用來對字符串或元組或列表中的元素進行去重操作。
生成一個集合可以使用如下語法:
生成集合語法1:
>>> l1=[1,2,3,4,5,6] >>> s1=set(l1) >>> print(s1) {1, 2, 3, 4, 5, 6}
在這里,使用工廠函數set創建集合,set的參數可以是一個列表,也可以是一個元組或字符串。
生成集合語法2:
>>> s2={6,7,8,9,10} >>> print(s2) {8, 9, 10, 6, 7}
生成集合語法3:
>>> s3={i for i in range(10)} >>> print(s3) {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
集合類型的方法和操作:
add
為集合增加一個元素,如果集合中本來已經存在這個元素對集合無影響 Add an element to a set. This has no effect if the element is already present.
>>> s1={1,2,3,4,5,6,7} >>> s1.add(8) >>> print(s1) {1, 2, 3, 4, 5, 6, 7, 8} >>> s1.add(9) >>> print(s1) {1, 2, 3, 4, 5, 6, 7, 8, 9}
clear
清空集合里所有的元素 Remove all elements from this set.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9} >>> s1.clear() >>> print(s1) set() >>> s2.clear() >>> print(s2) set()
copy
對集合進行淺拷貝(只復制元素,不復制內存地址) Return a shallow copy of a set.
>>> s1={1,2,3,4,5,6,7} >>> print(s1,id(s1)) {1, 2, 3, 4, 5, 6, 7} 140509859430472 >>> s2=s1.copy() >>> print(s2,id(s2)) {1, 2, 3, 4, 5, 6, 7} 140509842716712
difference
求兩個或多個集合的差集,并返回一個新集合 Return the difference of two or more sets as a new set.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s1.difference(s2) {1, 2, 3, 4} >>> s2.difference(s1) {8, 9, 10}
difference_update
把兩個集合的交集部分從集合中移除 Remove all elements of another set from this set.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s1.difference_update(s2) >>> print(s1) {1, 2, 3, 4} >>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s2.difference_update(s1) >>> print(s2) {8, 9, 10}
discard
從集合中移除一個元素,如果被移除的元素不在集合中,不會報錯 Remove an element from a set if it is a member. If the element is not a member, do nothing.
{1, 2, 3, 4, 5, 6, 7} >>> s1.discard(7) >>> print(s1) {1, 2, 3, 4, 5, 6} >>> s1.discard(4) >>> print(s1) {1, 2, 3, 5, 6} >>> print(s1) {1, 2, 3, 5, 6}
intersection
求兩個或多個集合中的交集 Return the intersection of two sets as a new set.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s1.intersection(s2) {5, 6, 7} >>> s2.intersection(s1) {5, 6, 7}
intersection_update
把兩個集合的交集做為新的集合 Update a set with the intersection of itself and another.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s1.intersection_update(s2) >>> print(s1) {5, 6, 7} >>> print(s2) {5, 6, 7, 8, 9, 10} >>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s2.intersection_update(s1) >>> print(s2) {5, 6, 7} >>> print(s1) {1, 2, 3, 4, 5, 6, 7}
isdisjoint
兩個集合沒有交集則返回True Return True if two sets have a null intersection.
>>> s1={1,2,3,4,5,6,7} >>> s2={5,6,7,8,9,10} >>> s1.isdisjoint(s2) False >>> s1={1,2,3,4} >>> s2={6,7,8,9} >>> s1.isdisjoint(s2) True
issubset
如果本集合是參數集合的子集,返回True Report whether another set contains this set.
>>> s1={1,2,3,4} >>> s2={1,2,3,4,5,6,7} >>> s1.issubset(s2) True >>> s2.issubset(s1) False
issuperset
如果本集合是參數集合的超集,返回True Report whether this set contains another set.
>>> s1={1,2,3,4} >>> s2={1,2,3,4,5,6,7} >>> s1.issuperset(s2) False >>> s2.issuperset(s1) True
pop
從集合中移除一個元素,如果集合為空,則報錯 Remove and return an arbitrary set element. Raises KeyError if the set is empty.
>>> s1={2,3,4,5} >>> s1.pop() 2 >>> print(s1) {3, 4, 5} >>> s1.pop() 3 >>> s1.pop() 4 >>> s1.pop() 5 >>> s1.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set'
remove
移除集合中的一個元素,如果集合是空的,則報錯 Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
>>> s1={1,2,3,4,5,6} >>> s1.remove(4) >>> print(s1) {1, 2, 3, 5, 6} >>> s1.remove(9) Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 9
symmetric_difference
返回兩個集合的對稱差集的集合 Return the symmetric difference of two sets as a new set.
>>> s1={1,2,3,4} >>> s2={6,7,8,9} >>> s1.symmetric_difference(s2) {1, 2, 3, 4, 6, 7, 8, 9} >>> s3={1,2,3,4,5,6} >>> s4={5,6,7,8,9,10} >>> s3.symmetric_difference(s4) {1, 2, 3, 4, 7, 8, 9, 10}
symmetric_difference_update
與參數集合做對稱差集,并返回給自身 Update a set with the symmetric difference of itself and another.
>>> s1={1,2,3,4} >>> s2={6,7,8,9} >>> s2.symmetric_difference_update(s1) >>> print(s2) {1, 2, 3, 4, 6, 7, 8, 9} >>> s3={1,2,3,4,5,6} >>> s4={5,6,7,8,9,10} >>> s3.symmetric_difference_update(s4) >>> print(s3) {1, 2, 3, 4, 7, 8, 9, 10}
union
求兩個或多個集合的并集 Return the union of sets as a new set.
>>> s1={1,2,3,4,5,6} >>> s2={5,6,7,8,9} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> s3={1,2,3,4} >>> s4={6,7,8,9} >>> s3.union(s4) {1, 2, 3, 4, 6, 7, 8, 9}
update
與另一個集合求并集,并返回給自身 Update a set with the union of itself and others.
>>> s3={1,2,3,4} >>> s4={6,7,8,9} >>> s3.update(s4) >>> print(s3) {1, 2, 3, 4, 6, 7, 8, 9}
看完上述內容,你們對Python中什么是set集合有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。