The following methods have been tested in the python interpreter, copy the code reader, remember uncomment.
# ! / Usr / bin / env Python # - * - Coding: UTF-8 - * - # !!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # The first set: set variable # ** **************** set by the class objects created ****************** # 1: format collection: use braces } or {set () function creates a collection # definition: 1, different elements; 2, disorder; 3, elements of the collection must be immutable type (numeric, string, a tuple) # Note: Create an empty set It must be set () instead of {}, {} as is used to create an empty dictionary # SE1 = {. 11, "qwer", (11,22,)} # SE2 = SET ((11,22,33)) when #set create a collection, in brackets must be iterative object # Print (SE1, SE2) # 2: set is iterative # SE = SET ( "qeyqi2321oi3h") # for I in SE: # Print (I) # 3: Other collection methods in, Not in, len () # SE = SET ( "qeyqi2321oi3h") # Print (SE 2 in) # Print (len (SE)) # Method ****************** set class provided ************************************************** # . 1: the Add () method : add an element to the collection # SE = {. 11, "qwer", (11,22,)} # S = se.add (2222) # in parentheses must be hashed object # Print (SE) # 2: Clear () method: Clear set # SE = {. 11, "qwer", (11,22,)} # Print (SE) # se.clear () # Print (SE) # . 3: Copy () method: shallow copy set # SE1 = {. 11, "qwer", (11,22,)} # SE2 = se1.copy () # Print (SE2) # 4: Delete the collection element # S = {. 11, "22 is", ( "qwer", 5,6,), "qwer"} # S1 = s.remove (. 11) #remove delete specific elements to be deleted when the element does not exist, an error; None return value # Print (S, S1) # # S = {. 11, "22 is", ( "qwer", 5,6,), "qwer"} # when s2 = s.discard (11) #discard delete specific elements, elements to be deleted does not exist, does not error; no return value # Print (S, S2) # # S = {. 11, "22 is", ( "qwer", 5,6,), "qwer"} # s.pop () # delete a random element, the return value # Print (S, s.pop () ) # 5: Update () method: to modify the current collection, you can add new elements to the current collection or collection # once, if ignored repeated elements add already exists in the collection, the element will only appear # SE1 the SET = ( "123456") # Print (SE1) # SE2 = the SET ( "qwerdf") # se2.update (SE1) # Print (SE2) # . 6: issubset () Method: Analyzing se1 se2 is not a subset of, and return bool value # se1 = {11,22,33} # se2 11,22} = { # Print (se2.issubset (se1)) # . 7: issuperset () Method: Analyzing se1 se2 is not a superset of, and return bool value # se1 = {11,22,33} # se2 11,22} = { # Print (se1.issuperset (se2)) # . 8: isdisjoint () method: there is no intersection of two sets is determined, no return True # SE1 = {11,22,33} # SE2 = {11,22} # Print (se1.isdisjoint (SE2)) # SE3 = 44,55} { # SE4 = {66, 77} # Print (se3.isdisjoint (SE4)) # ****************** set of commonly used relational operators ****************** # 1: Find the intersection of the sets # SE1 = {1,2, "Hello",. 3} # SE2 = {3,4, 5, "Hello"} # Print (SE1, SE2) # Print (se1.intersection (SE2)) # Print (& SE1 SE2) # se2.intersection_update (SE1) #intersection_update method: find the intersection, and the intersection with the cover SE2 # Print (SE1, SE2) # 2: using sets and set # SE1 = {1,2, "Hello",. 3} # SE2 = {3,4, 5, "Hello"} # Print (SE1, SE2) # Print (se1.union ( SE2)) # Print (SE1 | SE2) # 3: set difference using sets # SE1 = {1,2, "Hello", 3} # SE2 = {3,4, 5, "Hello"} # Print (SE1, SE2) # Print (se1.difference (se2)) # se1 removed and the rear portion of the same se2 remaining part # (se1-se2) Print # Print (se2.difference (se1)) # se2 after removing the same part of the remaining portion se1 # Print (se1-se2) # Se2.difference_update (SE1) #difference_update Method: seeking and set, and covered with the current collector and SE2 # Print (SE1, SE2) # 4: Cross complement using sets # SE1 = {1,2, "Hello",. 3} # SE2 = {3,4, 5, "Hello"} # Print (SE1, SE2) # Print (se1.symmetric_difference (se2)) # returns two elements will not be repeated in the collection set, it will remove two sets of elements are present. # Print (SE1 ^ se2) # se2.symmetric_difference_update (SE1) #symmetric_difference_update Method: seeking cross complement, and complement the cross covered with se2 # Print (SE1, se2) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!! # the second category set: a set of immutable # Note: 1: immutable set it can not increase the element deletion set, the contents of the set are immutable, similar to a string, a tuple # 2: immutable set, in addition to the content can not be changed, other functions and operations of the variable with the same set of set # S1 = frozenset ( "qwer1234") # incoming sequence is not generated variable set # S2 = frozenset ({11,22,33}) # incoming set, generating a set of immutable # S3 = frozenset () # define the empty set of immutable