Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets). Sets supports X in Set, Len (set), and for x in SE T. As an unordered collection, sets does not record the element position or insertion point. Therefore, sets does not support the operation of indexing, slicing, or other class sequences (Sequence-like). Here's a little simple example to illustrate the point. >>> x = set (' spam ') >>> y = set ([' H ', ' A ', ' m ']) >>> x, Y (Set ([' A ', ' P ', ' s ', ' M ']), set ([' A ', ' H ', ' m ']) to small applications. >>> X & y # Intersection set ([' A ', ' m ']) >>> x | Y # and set set ([' A ', ' P ', ' s ', ' h ', ' m ']) >>> x-y # Difference Set set ([' P ', ' s ']) remember the previous netizen asked how to remove the huge list of repeating elements, with a hash to solve the line, but the performance is not very high, It is very good to solve with set, such as:>>> a = [11,22,33,44,11,22]>>> B = Set (a) >>> bset ([One, one, A,]) >> > c = [i-I in b]>>> c[33, 11, 44, 22] It's cool, and a couple of lines can be done. 1.8 Set collection is used to contain a set of unordered objects. To create a collection, you can use the set () function and provide a series of items like this: s = set ([3,5,9,10]) #创建一个数值集合t = set ("Hello") #创建一个唯一字符的集合 unlike lists and tuples, collections are unordered and cannot Indexed by a number. Additionally, the elements in the collection cannot be duplicated. For example, if you examine the value of the T collection in the previous code, the result would be: >>> tset ([' H ', ' e ', ' l ', 'O ']) Note that only one ' l ' appears. A collection supports a range of standard operations, including the set, intersection, difference set, and symmetric difference sets, for example: A = T | S # T and S of the set B = t & S # T and s intersection c = t–s # differential Set (item in T, but not in s) d = t ^ S # symmetric difference set (item in T or s) , but not both) basic operations: T.add (' x ') # Add a s.update ([10,37,42]) # Add multiple items in S to remove () to delete an item: T.remove (' H ') Len (s) Set long The degree x in s tests if X is a member of S X isn't in s test if X is not a member of S S.issubset (t) s <= t tests if every element in S is in T S.issuperset (t) s >= t test if T is in every One element is in S s.union (t) s | T returns a new set containing each element in S and T S.intersection (t) S & T Returns a new set containing the common elements in S and T s.difference (t) s-t returns a new set containing S but T The element S.symmetric_difference (t) s ^ t returns a new set containing non-repeating elements in S and T S.copy () returns a shallow copy of set "s" Note: Union (), intersection (), Diffe Rence () and symmetric_difference () are non-operators (non-operator, which are shapes such as s.union (), will accept any iterable as parameters. Instead, their version of the operator (operator based counterparts) requires that the parameter must be sets. This avoids potential errors such as using Set (' abc ') & ' CBS ' instead of Set (' abc ') for more readable use. Intersection (' CBS '). Changes made from version 2.3.1: All previous parameters must be sets. In addition, both set and Immutableset support the comparison between set and set. Set of twoS is equal only in this case: the elements in each set are the elements in the other (the two are subset). A set is smaller than another set, only when the first set is the subset of the second set (it is a subset, but not equal). A set is hit by another set, only when the first set is the superset of the second set (it is a superset, but not equal). Sub-set and equality comparisons do not produce a complete sorting function. For example: Any two sets are not equal or sub-set, so the following operations will return FALSE:A<B, A==b, or a>b. Therefore, sets does not provide a __cmp__ method. Because sets only defines a partial sort function (subset relationship), the output of the List.sort () method is not defined for the list of sets. Operator operation result Hash (s) returns the hash value of s The following table lists the operations that are available for Set two for Immutableset: the operator (Voperator) is equivalent to the result of the operation S.update (t) s |= t returns the added Set "T" after the element of the set "s" s.intersection_update (t) s &= T returns only the set "s" that contains the elements in the set "T" S.difference_update (t) s-= t returns the deletion of the set "T" contained in the The element after the set "s" s.symmetric_difference_update (t) s ^= T returns a set "S" S.add (x) that contains a set "T" or a set "s" with the element instead of both, adding an element to the set "s" Xs.rem Ove (x) removes element x from set "s" and throws Keyerrors.discard if it does not exist (x) if element x exists in set "s", delete S.pop () deletes and returns an indeterminate element in set "s", if NULL, raises keyer Rors.clear () Delete all elements in set "s" Note: The non-operator version of Update (), Intersection_update (), Difference_update (), and Symmetric_difference_ Update () will accept any iterable as a parameter. From Changes made by the 2.3.1 version: all previous parameters must be sets. Also note: This module also contains a union_update () method, which is an alias for the update () method. This method is included for backwards compatibility. Programmers should use the update () method more, because this method is also supported by the built-in set () and Frozenset () types.
Python collection (set) type of operation (GO)