This article explains how to add (add element) the python3set method function)
Name = set (['Tom ', 'Lucy', 'Ben']) name. add ('juny') print (name) # output: {'Lucy ', 'juny', 'Ben', 'Tom '}
Clear (clear all elements)
Name = set (['Tom ', 'Lucy', 'Ben']) name. clear () print (name) # output: set ()
Copy (copy set)
Name = set (['Tom ', 'Lucy', 'Ben']) new_name = name. copy () print (new_name) # output: {'Tom ', 'Lucy', 'Ben '}
Difference (returns two or more sets of different elements and generates a new set)
A = set([2,3,4,5= set([3,4= set([2=
Difference_update (delete the elements in set A that exist in set B .)
A = set ([2, 3, 4, 5]) B = set ([4, 5]) A. difference_update (B) print (A) # output: {2, 3}
Discard (remove element)
N = set ([2, 3, 4]) n. discard (3) print (n) # output: {2, 4}
Intersection (take the intersection and generate a new set)
N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) n = n1.intersection (n2) print (n}
Intersection_update (take the intersection and modify the original set)
N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) n1.intersection _ update (n2) print (n1) # output: {4, 5}
Isdisjoint (determines the intersection. False is returned; True is returned)
N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) print (n1.isdisjoint (n2) # output: False
Issubset)
A = set ([2, 3]) B = set ([2, 3, 4, 5]) print (A. issubset (B) # output: True # A is A subset of B
Issuperset (determine parent set)
A = set ([2, 3]) B = set ([2, 3, 4, 5]) print (B. issuperset (A) # output: True # B is the parent set of
Pop (remove an element randomly)
N = set ([2, 3, 4, 5]) n1 = n. pop () print (n, n1) # output: {3, 4, 5} 2
Remove (remove a specified element)
N = set ([2, 3, 4, 5]) n. remove (2) print (n) # output: {3, 4, 5}
Symmetric_difference (intersection and generate a new set)
A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]) print (. symmetric_difference (B) # output: {2, 3, 6, 7}
Symmetric_difference_update (take the intersection and change the original set)
A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]). symmetric_difference_update (B) print (A) # output: {2, 3, 6, 7}
Union (obtains the union set and generates a new set)
A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]) print (. union (B) # output: {2, 3, 4, 5, 6, 7}
Update (take the union set and change the original set)
A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]). update (B) print (A) # output: {2, 3, 4, 5, 6, 7}
The above is an example to illustrate the features of the python3 set method. For more information, see other related articles in the first PHP community!