1, a collection of understanding
definition:
s={1,2,3,4,5}
s=set("hello")
s=set(["steven","job","dave"])
When defined by the set and iterator object is equivalent to the implementation for the loop
Features:
Different elements
Disorderly
Elements of the collection must be immutable (string, number, ancestral)
2, a common method
.add ( "a") # add an element
.clear () # Empty
.copy () # shallow copy
.pop () # random puncturing
.remove ( "job") # delete specified, you will get an error when deleting element does not exist
.discard ( "job") # delete specified, does not complain when you remove the element does not exist
3, the basic operation
Intersection, union, difference
a_s={"steve","job","dave","max"} b_s={"job","dave","mark"} print(a_s,b_s) # Intersection of Print (a_s.intersection (B_S)) # written. 1 Print (A_S & B_S) # writing 2 # Seeking union Print (a_s.union (B_S)) # written 1 Print (A_S | B_S) # writing 2 # Differencing sets, corresponding to the presence on the left, but not in the right set Print (a_s.difference (B_S)) # written. 1 Print (A_S-B_S) # writing 2 Print (B_S-A_S)
result:
{'dave', 'steve', 'max', 'job'} {'dave', 'mark', 'job'} {'dave', 'job'} {'dave', 'job'} {'job', 'dave', 'mark', 'steve', 'max'} {'job', 'dave', 'mark', 'steve', 'max'} {'steve', 'max'} {'steve', 'max'} {'mark'}
4, other methods
a_s={"steve","job","dave","max"} b_s={"job","dave","mark"} c_s={"job","dave"} # Cross complement, the complement of the intersection, and the set corresponding to a set of parts less intersection Print (a_s.symmetric_difference (B_S)) # written. 1 Print (A_S ^ B_S) # 2 written # result: { 'mark' , 'max', 'steve' } # .Difference_update () a_s.difference_update (B_S) # corresponds A_S-B_S = A_S Print (A_S) # result: { 'max', 'steve '} # .Isdisjoint () determines there is no intersection, the intersection did not return to true Print (a_s.isdisjoint (B_S)) # result: false # .Issubset () whether a subset of a set. Equivalent of S1 <S2 = Print (c_s.issubset (A_S)) # written. 1 Print (C_S <= A_S) # written 2 # .issuperset () whether a superset of the set. Equivalent of S1> S2 = Print (a_s.issuperset (C_S)) # result is true # .Update () you can add multiple values. Tuples can pass, lists iterables a_s.update (B_S) Print (A_S) b_s.update(("bob","zhou")) print(b_s) #结果为: #{'steve', 'job', 'max', 'dave', 'mark'} #{'bob', 'mark', 'job', 'dave', 'zhou'}
""" set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set. This has no effect if the element is already present. """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. """ pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. """ pass def difference(self, *args, **kwargs): # real signature unknown """ Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ pass def discard(self, *args, **kwargs): # real signature unknown """ Remove an element from a set if it is a member. If the element is not a member, do nothing. """ pass def intersection(self, *args, **kwargs): # real signature unknown """ Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. """ pass def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass def remove(self, *args, **kwargs): # real signature unknown """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. """ pass def symmetric_difference(self, *args, **kwargs): # real signature unknown """ Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) """ pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. """ pass def union(self, *args, **kwargs): # real signature unknown """ Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. """ passseset