Collection (set): is a collection of unordered sequence, the set of elements can be any data type; manifestation is set (set of elements), the automatic de-duplication; must be a set of incoming hashable type value, (value can not be stored in the dictionary format); and creating a set time is not able to represent this set1 = {}, this representation is not an empty set, which means that the dictionary is empty;
1. Create a collection
Declared directly #:
setl = {1,2,3,4}
List1 = [1,2,3,4,1,2,3,4 ] setl = SET (List1) # list incoming Print (setl) # return value: {1, 2, 3, 4}
2. The collection of operational relations
Intersection: Take two identical sets of elements constituting a new set
# Set intersection of setl = {1,2,3,4,5,6, ' C ' , ' D ' } SET2 = { ' A ' , ' B ' , ' C ' , ' D ' , 1,6 } Print (setl & SET2) # returned result is: {1, 'c', 'd', 6}
And Set: taking all the elements constituting two sets of a new set of identical elements removed
# Set request and sets setl = {1,2,3,4,5,6, ' C ' , ' D ' } SET2 = { ' A ' , ' B ' , ' C ' , ' D ' , 1,6 } Print (setl | SET2) # returned as the result: { 'b', 1, 2, 3, 4, 5, 6, 'a', 'd', 'c'}
Difference-set: -B A collection of collection, the collection element A is removed in set B, set A only taking element is not set B, and generates a new set
# Set of differencing sets setl = {1,2,3,4,5,6, ' C ' , ' D ' } SET2 = { ' A ' , ' B ' , ' C ' , ' D ' , 1,6 } Print (setl - SET2) # returned result is: {2, 3, 4, 5}
Non-collection: taking intersection of two sets, then taken in accordance with the complement of the intersection of the two sets, and finally set taking complement
# Set of non-seeking set setl = {1,2,3,4,5,6, ' C ' , ' D ' } SET2 = { ' A ' , ' B ' , ' C ' , ' D ' , 1,6 } Print (setl ^ SET2) # returns the result of: {2, 3, 4, 5, 'a', 'b'}
3. The method of determining a set of
# Judge whether this collection is collection contains additionally Print (set1.issubset (set2)) # If set2 set1 collection element contains a True otherwise it returns False # judge whether this collection contains another collection Print (set1.issuperset (set2 )) # If set1 set2 collection element contains a True otherwise it returns False # If the intersection of two sets is empty returns True Print (set1.isdisjoint (set2))