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}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1916/viewspace-2837282/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python之函式的相關介紹Python函式
- Python之logging模組相關介紹Python
- python之pymsql模組相關介紹PythonSQL
- Python學習之set集合Python
- Python set(集合)Python
- Nginx 相關介紹Nginx
- Drupal建站的相關介紹
- python-集合setPython
- python-資料型別之set集合Python資料型別
- 設計模式的相關介紹設計模式
- camunda相關資料介紹
- RTSP 流相關工具介紹
- python 集合型別 setPython型別
- 【JavaSE】集合類Collection集合Map集合的簡單介紹,List介面,中三個常用子類ArrayList、Vector、LinkedList之間的比較。Set介面。Java
- Python import相關內容區別介紹( import *** as 、from***import )PythonImport
- Android多渠道打包相關介紹Android
- Go gRPC 系列一:相關介紹GoRPC
- weex-toolkit工具的相關命令介紹
- Android Audio HAL 介面介紹之 adev_set_parameters()和out_set_parameters()Androiddev
- spark相關介紹-提取hive表(一)SparkHive
- 集合的介紹(正在更新中)
- JavaScript元素集合介紹JavaScript
- java集合類介紹Java
- map、unordered_map、set 和 unordered_set的小介紹
- java Set相關總結Java
- Python&Redis 無序集合set、有序集合zset操作PythonRedis
- 郵件安全相關開源軟體的介紹
- ArrayList相關方法介紹及原始碼分析原始碼
- 隱私計算相關技術介紹
- Python之str內部功能的介紹Python
- 簡單介紹Vue之vue.$set()方法原始碼案例Vue原始碼
- 有關Dialog類的相關資訊【對話方塊的介紹】
- Python 關於JSON模組介紹PythonJSON
- SAP WebClient UI和business switch相關的邏輯介紹WebclientUI
- Python資料分析--Numpy常用函式介紹(5)--Numpy中的相關性函式Python函式
- 以太坊:Dapp及相關開發工具介紹APP
- Spread表格元件For JAVA功能介紹—表格相關操作元件Java
- OceanBase學習之路10|C 相關 API 介紹API