創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!

今天就跟大家聊聊有關(guān)Python中什么是set集合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
認(rèn)識(shí)python中的set集合及其用法
python中,集合(set)是一個(gè)無序排列,可哈希,支持集合關(guān)系測(cè)試,不支持索引和切片操作,沒有特定語法格式,只能通過工廠函數(shù)創(chuàng)建.集合里不會(huì)出現(xiàn)兩個(gè)相同的元素,所以集合常用來對(duì)字符串或元組或列表中的元素進(jìn)行去重操作。
生成一個(gè)集合可以使用如下語法:
生成集合語法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è)元組或字符串。
生成集合語法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
為集合增加一個(gè)元素,如果集合中本來已經(jīng)存在這個(gè)元素對(duì)集合無影響 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} 140509842716712difference
求兩個(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è)集合沒有交集則返回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)
Trueissubset
如果本集合是參數(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)
Falseissuperset
如果本集合是參數(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)
Truepop
從集合中移除一個(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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
KeyError: 9symmetric_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è)資訊頻道,感謝大家的支持。
新聞標(biāo)題:Python中什么是set集合-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article16/dcpedg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、建站公司、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容