Python Learning Note 5.1-core type-set set type [go]

Source: Internet
Author: User
Tags iterable new set

Transferred from: http://blog.csdn.net/business122/article/details/7541486

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. Sets 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. 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#intersectionSet (['a','m'])>>> x | Y#and setSet (['a','P','s','h','m'])>>> X-y#Difference SetSet (['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 very good, examples are as follows:>>> a = [11,22,33,44,11,22]>>> B =set (a)>>>bset ([33, 11, 44, 22])>>> C = [i forIinchb]>>>c[33, 11, 44, 22] It's cool, and a couple of lines will be done. 1.8A collection 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])#create a collection of valuesT= Set ("Hello")#Create a collection of unique charactersUnlike 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:>>>Tset (['H','e','L','o']) Notice that there is only one'L'. The collection supports a range of standard operations, including the set, intersection, difference set, and symmetric difference sets, for example: a= T | S#the set of T and Sb= T & S#intersection of T and SC= T–s#differential Set (items 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 an itemS.update ([10,37,42])#adding multiple items in Suse Remove () to delete an item: T.remove ('H'Len (s) set length xinchs tests if X is a member of S X not inchs tests if X is not a member of S S.issubset (t) s<=T Test if every 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 new set containing an element in s but not in T S.symmetric_difference (t) s^T returns a new set containing the non-repeating elements in S and T S.copy () returns a shallow copy of the set "s" Note: Union (), intersection (), difference (), and Symmetric_difference ( ) is a non-operator (non-operator, that is, a version like 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. 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<b, A==b, or a>b. Therefore, sets does not provide__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 after adding the element in Set "T "&=T returns only the set "s" s.difference_update (t) s that contain the elements in the set "T "-=T returns the Set "S" s.symmetric_difference_update (t) s after removing the element contained in set "T "^=T returns a set "S" that contains a set "T" or a set "s" with an element other than both, S.add (x) adds an element Xs.remove (x) to the set "s", removes the element x from the set "s", and throws the KEYERRORS.D if it does not exist. Iscard (x) if element x exists in set "s", then delete S.pop () deletes and returns an indeterminate element in set "s", if Null throws Keyerrors.clear () deletes all elements in set "s" Note: Non-operator version of UPD Ate (), intersection_update (), Difference_update (), and Symmetric_difference_update () will accept any iterable as parameters. From2.3.1changes to the 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 Learning Note 5.1-core type-set set type [go]

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.