Go Python collection (set) types of operations

Source: Internet
Author: User
Tags iterable new set shallow copy

1 Python's set, like other languages, is a set of unordered, non-repeating elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports Union (union), intersection (intersection), Difference (poor), and sysmmetric Difference (symmetric difference set) and other mathematical operations.2 3Sets support XinchSet, Len (set), and forXinchSet As an unordered collection, sets does not record the element position or insertion point. Therefore, sets does not support indexing, slicing, or other class sequences (sequence-Like ) operation. 4 5  6 7 Here 's a little simple example to illustrate the point. 8 9>>> x = Set ('spam')Ten>>> y = set (['h','a','m']) One>>>x, y A(Set (['a','P','s','m']), set (['a','h','m'])) -  - come back to the little apps.  the  ->>> X & Y#intersection -Set (['a','m']) -  +>>> x | Y#and set -Set (['a','P','s','h','m']) +  A>>> X-y#Difference Set atSet (['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, with set to solve or is very good, examples are as follows: -  ->>> a = [11,22,33,44,11,22] ->>> B =set (a) in>>>b -Set ([33, 11, 44, 22]) to>>> C = [i forIinchb] +>>>C -[33, 11, 44, 22] the  * It's cool, you can do it in a few lines.  $ Panax Notoginseng1.8Collection -   the The 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 as follows: +  A   the  +s = set ([3,5,9,10])#create a collection of values -  $t = Set ("Hello")#Create a collection of unique characters $  -   -  the Unlike lists and tuples, 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: - Wuyi   the  ->>>T Wu  -Set (['H','e','L','o']) About  $   -  -Notice that there's only one'L'.  -  A A collection supports a range of standard operations, including the set, intersection, difference set, and symmetric difference sets, such as: +  the   -  $A = T | S#the set of T and S the  theb = t & S#intersection of T and S the  thec = T–s#differential Set (items in T, but not in s) -  inD = t ^ s#symmetric difference sets (items in t or S, but not both) the  the   About  the Basic Operation: the  theT.add ('x')#Add an item +  -S.update ([10,37,42])#adding multiple items in S the Bayi   the  the use Remove () to delete an item: -  -T.remove ('H') the  the   the  the Len (s) - the length of the set the  theXinchs the Test if X is a member of S94  theX not inchs the Test if X is not a member of S the 98 S.issubset (t) AboutS <=T - Test if every element in S is in t101 102 S.issuperset (t)103S >=T104 tests if every element in T is in S the 106 s.union (t)107s |T108 returns a new set containing each element in S and T109  the s.intersection (t)111S &T the returns a new set containing the common elements in s and T113  the s.difference (t) theS-T the returns a new set containing elements in s but not in T117 118 s.symmetric_difference (t)119s ^T - returns a new set containing elements that are not duplicates in S and T121 122 s.copy ()123 returns a shallow copy of the set "s"124  the 126Note: Unions (), intersection (), difference (), and symmetric_difference () are non-operators (non-operator, which are shapes such as s.union (), 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 the use of set for more readable purposes ('ABC') &'CBS'To replace set ('ABC'). Intersection ('CBS')。 From 2.3.1changes made in the version: all previous parameters must be sets. 127  - 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). 129  theSub-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__cmp__method. 131  the 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. 133 134 135 operator136 result of Operation137 138 Hash (s)139 returns the hash value of S $ 141 142 The following table lists the operations that are not available for Immutableset for Set two:143 144 operator (voperator)145 equivalent to146 result of Operation147 148 s.update (t)149S |=T Max returns the set "S" after the element in set "T" is added151  the s.intersection_update (t)153S &=T154 returns only the set "s" containing the elements in set "T "155 156 s.difference_update (t)157S-=T158 returns the set "S" after deleting the element contained in set "T "159  the s.symmetric_difference_update (t)161S ^=T162 returns a set "s" containing elements in set "T" or set "s", rather than both163 164 s.add (x)165 166 add element x to set "s"167 168 s.remove (x)169  the remove element x from set "s" and throw keyerror if not present171 172 S.discard (x)173 174 if element x exists in set "s", delete175 176 S.pop ()177 178 deletes and returns an indeterminate element in the set "s" and throws a keyerror if it is empty179  the s.clear ()181 182 Delete all elements in set "s"183 184 185Note: The non-operator version of Update (), Intersection_update (), Difference_update (), and Symmetric_difference_update () will accept any iterable as parameters. From 2.3.1changes to the version: all previous parameters must be sets. 186 187 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. 188  189  


Go Python collection (set) types of operations

Related Article

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.