set (mathematics)
Features: a collection object is an unordered set of hashable values: collection members can be dictionary keys, and unlike lists and tuples, collections cannot be indexed by number. In addition, elements in a collection cannot be duplicated.
define
set() -> new empty set object set(iterable) -> new set object s = {0}
Application: De-weighting
>>> lst1 = [1,1,2,2,3,4,2] >>> list(set(lst1)) [1, 2, 3, 4]
Common Operations
Sets support a number of standard operations, including concatenation|, intersection&, differencing - and symmetric differencing ^
subsets < <= and supersets > >=
Adding, deleting, and clearing operations
See the following code example
>>> lst1 = [1,2] >>> lst2 = [2,3] >>> a = set(lst1) #Define the collection >>> b = set(lst2) >>> a,b ({1, 2}, {2, 3}) >>> a|b # Take the parallel set {1, 2, 3} >>> a&b # Take the intersection {2} >>> a-b # Take the difference set {1} >>> b-a # Take the difference set {3} >>> list(a) # Convert a collection to a list, or to a tuple, e.g. tuple(a) returns (1,2) [1, 2] >>> a < b #Subset judgment False >>> c = set([1]) >>> c {1} >>> c < a #Subset judgment True >>> c <= a #Subset judgment True >>> d = set([1,2,3]) >>> d > a # Superset judgment True >>> >>> d >= a # Superset judgment True >>> a,b ({1, 2}, {2, 3}) >>> a^b # Symmetric difference sets {1, 3} >>> c {1} >>> d {1, 2, 3} >>> a^d # Symmetric difference sets {3} >>> s = {0} >>> type(s) <class 'set'> >>> >>> print(s, len(s)) # Set length {0} 1 >>> ('1') #Add elements >>> s {0, '1'} >>> ([2,3]) # Add multiple elements >>> s {0, 2, 3, '1'} >>> (2) # Delete the specified element, or report an error if it's not there. >>> s {0, 3, '1'} >>> () #Randomly deleting elements (doesn't seem to help) 0 >>> s {3, '1'} >>> (3) # Delete the specified element >>> s {'1'} >>> () # Empty the collection >>> s set()