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之pymsql模組相關介紹PythonSQL
- 關於Python的集合setPython
- Python之logging模組相關介紹Python
- Python學習之set集合Python
- Nginx 相關介紹Nginx
- Python set(集合)Python
- 設計模式的相關介紹設計模式
- 使用NGUINGUI的相關介紹NGUI
- python-集合setPython
- RTSP 流相關工具介紹
- mvn相關介紹和命令
- [android]androguard相關介紹Android
- camunda相關資料介紹
- python-資料型別之set集合Python資料型別
- python基礎之字典dict和集合setPython
- SAP的月結相關流程介紹
- oracle goldengate 相關概念介紹OracleGo
- 機器學習,深度學習相關介紹機器學習深度學習
- python 集合型別 setPython型別
- javascript的節點相關內容介紹JavaScript
- weex-toolkit工具的相關命令介紹
- linux的vm相關引數介紹Linux
- 【JavaSE】集合類Collection集合Map集合的簡單介紹,List介面,中三個常用子類ArrayList、Vector、LinkedList之間的比較。Set介面。Java
- Python import相關內容區別介紹( import *** as 、from***import )PythonImport
- Go gRPC 系列一:相關介紹GoRPC
- spark相關介紹-提取hive表(一)SparkHive
- Android多渠道打包相關介紹Android
- 菜鳥朋友圈的相關介面介紹
- linux中時間相關的函式介紹Linux函式
- ssd相關的一些資料介紹
- java集合類介紹Java
- JavaScript元素集合介紹JavaScript
- java Set相關總結Java
- 集合相關面試題面試題
- 集合框架-Set集合框架
- javascript關於URI相關內容簡單介紹JavaScript
- 郵件安全相關開源軟體的介紹