Go: The Python collection (set) type operation

Source: Internet
Author: User

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 set. 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, only not The feeling is not very high in performance, with set resolution is very good, the example is as follows: >>> a = [11,22,33,44,11,22] >>> B = Set (a) >>> B set ([33,    One, one, []) >>> C = [I for i in B] >>> C [33, 11, 44, 22] It's cool, and a few lines will 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") #创建一个唯一字符的集合 With List and metaGroups, collections are unordered and cannot be indexed by numbers. Additionally, the elements in the collection cannot be duplicated.    For example, if you examine the value of the T collection in the preceding code, the result would be: >>> t set ([' H ', ' e ', ' l ', ' o ']) notice 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 |  The intersection of S # T and S-B = t & S # T and s intersect C = t–s # differential Set (item in T, but not in s) d = t ^ S # Symmetric difference sets (items in t or S, but not both) basic operation: T.add (' x ') # Add a s.update ([10,37,42]) # Add multiple items in s using Remov E () can delete an item: T.remove (' H ') Len (s) set length x in s test if X is a member of S X not in S test x is not a member of S S.issubset (t) s <= t test whether each element in S is in T S.issuperset (t) s >= t test if every element in T 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      The new set contains elements in s but not in T 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 (), difference (), and symmetric_difference () are non-operators (non-operator, which are shaped like s.The Union () version will accept any iterable as a parameter. 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. Two sets in this case it is equal only: 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&LT;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: operator (voperator) is equivalent to the result of the operation S . Update (t) s |= T returns the Set "S" s.intersection_update (t) s &= t that adds the element after the set "T" to return only the set "s" containing the elements in set "T" S.D Ifference_update (t) s-= t returns the Set "S" after removing the element contained in set "T" S.symmetric_difference_update (t) s ^= t returns a containing set "T" or SThe set "S" S.add (x) of elements in ET "s" that have not both have the element x S.remove (x) added to the set "s" to remove the element x from set "s" and, if not present, to raise the Keyerror S.dis Card (x) if element x exists in set "s", then delete S.pop () deletes and returns an indeterminate element in set "s", if null, throws Keyerror S.clear () deletes all of the set "s" Element Note: The non-operator version of Update (), Intersection_update (), Difference_update (), and Symmetric_difference_update () will accept any iterable as the parameter Number.    Changes made from version 2.3.1: 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.

  

Go: The Python collection (set) type operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.