程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Set module of Python notes

編輯:Python

#set( aggregate )
# Collection of unordered elements
# The element is the only , Duplicate elements are automatically filtered

01 Create set

#set() Create an empty set
st01 = set()
#set(iterable) Create collections using iteratable objects

lt = [1,2,3,4,5]
st = set(lt)
02 Add elements to the collection

#add(…)

st02 = set([1,2,3,4,5])
st02.add(6)
03 Multiply and divide all elements in the set

#clear(…)

st03 = set([1,2,3,4,5])
st03.clear()
04 Copy set

#copy(…)

st03 = set([1,2,3,4,5])
st = st03.copy()
05 Back to the assembly A Existing set B Elements that do not exist

#difference(…)

st05_1 = set([1,2,5])
st05_2 = set([0,1,2,3,4])
st = st05_1.difference(st05_2)
06 Using set A Existing set B Nonexistent element update collection A

#difference_update(…)

st06_1 = set([1,2,5])
st06_2 = set([0,1,2,3,4])
st06_1.difference_update(st06_2)
st = st06_1
07 Removes the specified element from the collection , If the element is not in the collection, there is no operation

#discard(…)

st07 = set([0,1,2,3,4,6])
st07.discard(7)
st = st07
08 Returns the intersection of two sets

#intersection(…)

st08_1 = set([0,1,2,3,4,6])
st08_2 = set([0,1,4,6,7,9])
st = st08_1.intersection(st08_2)
09 Update a set with the intersection of two sets A

#intersection_update(…)

st09_1 = set([0,1,2,3,4,6])
st09_2 = set([0,1,4,6,7,9])
st09_1.intersection_update(st09_2)
st = st09_1
10 Determine whether two sets do not intersect

#isdisjoint(…)

st10_1 = set([0,1,2,3,4,6])
st10_2 = set([0,1,4,6,7,9])
st = st10_1.isdisjoint(st10_2)
11 Judgment set A Is it a collection B Subset

#issubset(…)

st11_1 = set([0,4,6])
st11_2 = set([0,1,4,6,7,9])
st = st11_1.issubset(st11_2)
12 Judgment set A Is it a collection B Superset

#issuperset(…)

st12_1 = set([0,4,6,1,7,8,9])
st12_2 = set([0,1,4,6])
st = st12_1.issuperset(st12_2)
13 Pop an element from a non empty collection

#pop(…)

st13 = set([0,4,6,1,7,8,9])
st13.pop()
st = st13
14 Remove a member element from the collection

#remove(…)

st14 = set([0,4,6,1,7,8,9])
st14.remove(9)
st = st14
15 Back to the assembly A And assemble B Of A∪B-A∩B Part of

#symmetric_difference(…)

st15_1 = set([0,4,6,1,7,8,9])
st15_2 = set([0,1,4,6,3,2])
st = st15_1.symmetric_difference(st15_2)
16 Using set A And assemble B Of A∪B-A∩B Partial update set of A

#symmetric_difference_update(…)

st16_1 = set([0,4,6,1,7,8,9])
st16_2 = set([0,1,4,6,3,2])
st16_1.symmetric_difference_update(st16_2)
st = st16_1
17 aggregate A And assemble B Of A∪B

#union(…)

st17_1 = set([0,4,6,1,7,8,9])
st17_2 = set([0,1,4,6,3,2])
st = st17_1.union(st17_2)
18 Using set A And assemble B Of A∪B Update collection A

#update(…)

st18_1 = set([0,4,6,1,7,8,9])
st18_2 = set([0,1,4,6,3,2])
st18_1.update(st18_2)
st = st18_1
19 Other operations of the collection

st19 = set([0,4,6,1,7,8,9])
#len Get the collection size
len(st19)
#min max Get set extremum
min(st19)
max(st19)
#del Delete the collection
del st19


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved