創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),明山企業(yè)網(wǎng)站建設(shè),明山品牌網(wǎng)站建設(shè),網(wǎng)站定制,明山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,明山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。今天就跟大家聊聊有關(guān)Python中什么是set集合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
認(rèn)識(shí)python中的set集合及其用法
python中,集合(set)是一個(gè)無(wú)序排列,可哈希,支持集合關(guān)系測(cè)試,不支持索引和切片操作,沒(méi)有特定語(yǔ)法格式,只能通過(guò)工廠函數(shù)創(chuàng)建.集合里不會(huì)出現(xiàn)兩個(gè)相同的元素,所以集合常用來(lái)對(duì)字符串或元組或列表中的元素進(jìn)行去重操作。
生成一個(gè)集合可以使用如下語(yǔ)法:
生成集合語(yǔ)法1:
>>> l1=[1,2,3,4,5,6] >>> s1=set(l1) >>> print(s1) {1, 2, 3, 4, 5, 6}
在這里,使用工廠函數(shù)set創(chuàng)建集合,set的參數(shù)可以是一個(gè)列表,也可以是一個(gè)元組或字符串。
生成集合語(yǔ)法2:
>>> s2={6,7,8,9,10} >>> print(s2) {8, 9, 10, 6, 7}
生成集合語(yǔ)法3:
>>> s3={i for i in range(10)} >>> print(s3) {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
集合類型的方法和操作:
add
為集合增加一個(gè)元素,如果集合中本來(lái)已經(jīng)存在這個(gè)元素對(duì)集合無(wú)影響 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
對(duì)集合進(jìn)行淺拷貝(只復(fù)制元素,不復(fù)制內(nèi)存地址) 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
求兩個(gè)或多個(gè)集合的差集,并返回一個(gè)新集合 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
把兩個(gè)集合的交集部分從集合中移除 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
從集合中移除一個(gè)元素,如果被移除的元素不在集合中,不會(huì)報(bào)錯(cuò) 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
求兩個(gè)或多個(gè)集合中的交集 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
把兩個(gè)集合的交集做為新的集合 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
兩個(gè)集合沒(méi)有交集則返回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
如果本集合是參數(shù)集合的子集,返回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
如果本集合是參數(shù)集合的超集,返回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
從集合中移除一個(gè)元素,如果集合為空,則報(bào)錯(cuò) 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 "", line 1, in KeyError: 'pop from an empty set'
remove
移除集合中的一個(gè)元素,如果集合是空的,則報(bào)錯(cuò) 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 "", line 1, in KeyError: 9
symmetric_difference
返回兩個(gè)集合的對(duì)稱差集的集合 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
與參數(shù)集合做對(duì)稱差集,并返回給自身 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
求兩個(gè)或多個(gè)集合的并集 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
與另一個(gè)集合求并集,并返回給自身 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}
看完上述內(nèi)容,你們對(duì)Python中什么是set集合有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝大家的支持。