Basic Python and python tutorials
When Python executes a certain type of code, it is equivalent to executing the _ init _ method, for example, list () list _ init __
A set of unordered and non-repeating elements can be considered as a set in mathematics.
Usage: 1. create a set s = set () Create an empty set s = set ([11,22, 33]) s = set ('asdfghh') s = {'asd ', 'saf'} 2. access Collection a in s3. update collection s. add () --- add. Only one element s can be added at a time. clear () ---- clear s. upadte () --- update the Set and assign it to s. You can add multiple iterations, which is equivalent to executing the for loop and then repeating the. add method. S. discard () --- Delete the specified element. If the specified element does not exist, the error s. remove () --- Delete the specified element. If the specified element does not exist, the error s. pop () ----- Delete the element randomly
4. Join (|)
Union operations are equivalent to OR operations on the set. The union symbol has an equivalent method, union ().
>>> s1=set('begin')>>> s2=set('man')>>> s3=s1|s2>>> s3set(['a', 'b', 'e', 'g', 'i', 'm', 'n'])
5. Intersection (&)
It is equivalent to the set AND. The equivalent method of the intersection symbol is intersection ()
>>> s1&s2set(['n'])>>> s1.intersection(s2)set(['n'])
6. Check and supplement (-)
The equivalent method is difference ()
>>> s1-s2 # s1 - (s1 & s2)set(['i', 'b', 'e', 'g'])>>> s1.difference(s2)set(['i', 'b', 'e', 'g'])
7. Symmetric Difference (^)
Symmetric Difference is the XOR ('exclusive or ') of a set. The obtained element belongs to s1, s2, but not both s1 and s2. the equivalent method of Symmetric Difference ()
>>> s1^s2 #(s1 U s2)-(s1 & s2)set(['a', 'b', 'e', 'g', 'i', 'm'])>>> s1.symmetric_difference(s2)set(['a', 'b', 'e', 'g', 'i', 'm'])
Note: and, or between Sets
>>> S1 and s2 are equivalent to s2set (['A', 'M', 'n']) # s2 >>> s1 or s2 is equivalent to s1set (['I', 'B', 'E', 'G', 'n']). # obtain s1 >>>
5. Conversion between collections, lists, tuples, and strings
>>> List (s1) ['I', 'B', 'E', 'G', 'n'] >>> str (s1) "set (['I', 'B', 'E', 'G', 'n'])"> tuple (s1) ('I ', 'B', 'E', 'G', 'n ')
**************************************** * ****** The reference address is missing, sorry *************************************** ***********