This article mainly summarizes the Python3 set (set) of the syntax of the relevant data, the text gives a detailed example code, for everyone has a certain reference value, the need for friends below to see it together.
Introduced
Set Gu Ming, is a collection, the elements of the collection are unique, unordered. A {} contains elements that make up a set, which can be a variety of data types (but not a list, a collection, a dictionary, a tuple)
A collection is a set of unordered, non-repeating elements. Basic features include relationship testing and elimination of duplicate elements. The collection object also supports union (joint
Intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets), and other mathematical operations.
Curly braces or set () functions can be used to create collections. Note: To create an empty collection, you must use Set () instead of {}. {} to create an empty dictionary;
The specific syntax is summarized below.
Add (add Element)
name = set ([' Tom ', ' Lucy ', ' Ben ']) name.add (' Juny ') print (name) #输出: {' Lucy ', ' Juny ', ' Ben ', ' Tom '}
Clear (Empty all elements)
name = set ([' Tom ', ' Lucy ', ' Ben ']) name.clear () print (name) #输出: Set ()
Copy (copy set collection)
name = set ([' Tom ', ' Lucy ', ' Ben ']) new_name = Name.copy () print (new_name) #输出: {' Tom ', ' Lucy ', ' Ben '}
Difference (returns the different elements in two or more collections and generates a new collection)
A = Set ([2,3,4,5]) B = set ([3,4]) C = set ([2]) n = n1.difference (n2,n3) print (n) #输出: {5} #返回A集合里面, elements not in the B and C collections, and a new collection is generated
Difference_update (removes the element in the a collection that exists in the B collection. )
A = Set ([2,3,4,5]) B = set ([4,5]) a.difference_update (B) print (A) #输出: {2, 3}
Discard (remove Element)
n = set ([2,3,4]) N.discard (3) print (n) #输出: {2, 4}
Intersection (intersection is taken and a new collection is generated)
N1 = set ([2,3,4,5]) N2 = set ([4,5,6,7]) n = n1.intersection (n2) print (n) #输出: {4, 5}
Intersection_update (take intersection, modify the original collection)
N1 = set ([2,3,4,5]) N2 = set ([4,5,6,7]) n1.intersection_update (n2) print (n1) #输出: {4, 5}
Isdisjoint (judgment intersection, is return false, no return true)
N1 = set ([2,3,4,5]) N2 = set ([4,5,6,7]) print (N1.isdisjoint (n2)) #输出: False
Issubset (Judgment subset)
A = set ([2,3]) B = set ([2,3,4,5]) print (A.issubset (B)) #输出: True#a is a subset of B
Issuperset (judging the parent set)
A = set ([2,3]) B = set ([2,3,4,5]) print (B.issuperset (A)) # Output: True#b is the parent set of a
Pop (random removal of an element)
n = set ([2,3,4,5]) n1 = N.pop () print (N,N1) # output: {3, 4, 5} 2
Remove (removes the specified element)
n = set ([2,3,4,5]) N.remove (2) print (n) # output: {3, 4, 5}
Symmetric_difference (intersection is taken and a new collection is generated)
A = Set ([2,3,4,5]) B = set ([4,5,6,7]) print (A.symmetric_difference (B)) # output: {2, 3, 6, 7}
Symmetric_difference_update (Take the intersection, change the original set)
A = Set ([2,3,4,5]) B = set ([4,5,6,7]) a.symmetric_difference_update (B) print (A) # output: {2, 3, 6, 7}
Union (to take and generate a new set)
A = Set ([2,3,4,5]) B = set ([4,5,6,7]) print (A.union (B)) # output: {2, 3, 4, 5, 6, 7}
Update (Take the set, change the original collection)
A = Set ([2,3,4,5]) B = set ([4,5,6,7]) a.update (B) print (A) # output: {2, 3, 4, 5, 6, 7}
Summarize