Python's set

Source: Internet
Author: User

Set

Set set, which is an unordered and non-repeating collection of elements

    • The benefits of Set
Set access several fast set native solve data duplication problem
# The original old_dict in the database = {"#1": {' Hostname ':' C1 ',' Cpu_count ':2,' Mem_capicity ':80},"#2": {' Hostname ':' C1 ',' Cpu_count ':2,' Mem_capicity ':80},"#3": {' Hostname ':' C1 ',' Cpu_count ':2,' Mem_capicity ':80}}# new data reported by CMDB new_dict = {"#1": {' Hostname ':' C1 ',' Cpu_count ':2,' Mem_capicity ':800},"#3": {' Hostname ':' C1 ',' Cpu_count ':2,' Mem_capicity ':80},  "#4": { ' hostname ':  ' C2 ',  ' Cpu_count ': 2,  ' mem_capicity ': 80}}s1=set () Span class= "Hljs-keyword" >for i in old_dict.keys (): s1.< Span class= "hljs-built_in" >add (i) print (S1) s2=set () for J Span class= "Hljs-operator" >in new_dict.keys (): S2. Add (j) print (S2) s3=s2.difference (S1) print (S3) for n in s3:s= New_dict.get (n) old_dict.update ({n:s}) print (old_dict)      
HelpOnSetObjectClassSetObject) |Set ()NewEmptySetObject |Set (iterable),NewSetObject | |Build an unordered collectionOfUnique elements. | | Methods defined here: | | __AND__ (SelfValue,/) |Returnself&Value. | | __contains__ (...) | X.__contains__ (y) <==> yIn X. | | __EQ__ (SelfValue,/) |Returnself==Value. | | __GE__ (SelfValue,/) |Returnself>=Value. | | __GETATTRIBUTE__ (SelfName,/) |Return GetAttr (SelfName). | | __GT__ (SelfValue,/) |ReturnSelf>Value. | | __IAND__ (SelfValue,/) |Returnself&=Value. | | __INIT__ (Self,/, *args, **kwargs) | InitializeSelf. SeeHelpTypeSelf))For accurate signature. | | __IOR__ (SelfValue,/) |Returnself|=Value. | | __ISUB__ (SelfValue,/) |Returnself-=Value. | | __ITER__ (Self,/) | Implement ITER (Self). | | __IXOR__ (SelfValue,/) |Returnself^=Value. | | __LE__ (SelfValue,/) |Returnself<=Value. | | __LEN__ (Self,/) |ReturnLenSelf). | | __LT__ (SelfValue,/) |Returnself<Value. | | __NE__ (SelfValue,/) |Returnself!=Value. | | __new__ (*args, **kwargs)From Builtins.Type |CreateandReturn aNewObject. SeeHelpTypeFor accurate signature. | | __OR__ (SelfValue,/) |ReturnSelf|Value. | | __RAND__ (SelfValue,/) |Returnvalue&Self. | | __reduce__ (...) |Return State informationFor pickling. | | __REPR__ (Self,/) |Return repr (Self). | | __ROR__ (SelfValue,/) |Returnvalue|Self. | | __RSUB__ (SelfValue,/) |Returnvalue-Self. | | __RXOR__ (SelfValue,/) |Returnvalue^Self. | | __sizeof__ (...) | S.__SIZEOF__ ()Sizeof SInchMemoryInchbytes | | __SUB__ (SelfValue,/) |Returnself-Value. | | __XOR__ (SelfValue,/) |Returnself^Value. | |Add (...) |Add anElementto aSet. | | This haveNo effectIf theElementis alreadyPresent."' Adds an element to the set, and if it is a repeating element it will not be added ' | |Clear (...) | Remove all elementsFrom thisSet."Empty all elements inside set" | | Copy (...) |Return a shallow copyof aSet.' Shallow copy ' | |Difference (...) |Return theDifferenceof aor moreSetsAs aNewSet. | | (I.E. all elements.IsIn thisSet butNot the others.)"' Compares two or more sets, placing different elements into the new set, example: A, b two set contrast, return elements in a that are not present in B to a new collection >>> s1={1,2,3,4}>>> s2={1,2} >>> s3=s1.difference (S2) >>> print (S3) {3, 4} "| | Difference_update (...) | Remove all elementsof anotherSetFrom thisSet."' Removes the same element from the current collection as in B. Note: Modify the current collection directly >>> s1={1,2,3,4}>>> s2={1,2,}>>> s1.difference_update (S2) >>> print ( S1) {3, 4} "| | Discard (...) | Remove anElementFrom aSetIf itis aMember. | |If theElementIsNot aMemberDoNothing."If the element belongs to the collection, delete the current element if it does not belong to, unchanged >>> s1={1,2,3,4}>>> S1.discard (9) >>> print (S1) {1, 2, 3, 4}> >> S1.discard (1) >>> print (S1) {2, 3, 4} "| | Intersection (...) |Return the intersectionof aSetsAs aNewSet. | | (I.E. all elements.IsInchBothSets.)"Take the intersection of two sets and return to a new collection >>> s1={1,2,3,4}>>> s2={1,2,5,6}>>> s3=s1.intersection (s2) > >> Print (S3) {1, 2} "| | Intersection_update (...) |Update ASetWith the intersectionof itselfand another. |"Takes two sets of intersections, assigns values to the current collection >>> s1={1,2,3,4}>>> s2={1,2,5,6}>>> s1.intersection_update (s2) > >> Print (S1) {1, 2} "| Isdisjoint (...) |ReturnTrueIf theSets has aNull intersection."' Determine if two sets have intersections, if there is a return of false, no then return ture>>> s1={1,2,3,4}>>> s2={1,2,5,6}>>> s3={9,10}> >> s1.isdisjoint (S2) false>>> s1.isdisjoint (S3) True ' ' | | Issubset (...) | Report whether anotherSet contains thisSet."' Judge if the former is not a subset of the later set, yes: Tute, no:false>>> s1={1,2,3,4}>>> s2={1,2,3,4,5,6,7}>>> s3={1,2}> >> s1.issubset (S2) true>>> S1.issubset (S3) False ' ' | | Issuperset (...) | Report whether thisSet contains anotherSet."' Determine if the former is the parent set of the latter, is: Tute, no:false>>> s1={1,2,3,4}>>> s2={1,2,3,4,5,6,7}>>> s3={1,2}> >> s1.issuperset (S2) false>>> S1.issuperset (S3) True ' ' | | Pop (...) | Removeandreturn an arbitrarySetElement. | Raises keyerrorIf theSetIsEmpty."' Deletes the elements in the collection and returns the deleted elements (randomly deleted? ) ' | | Remove (...) | Remove anElementFrom aSet It must be a member. | | If the element is not a member, raise a keyerror. "' Deletes the specified element, if not present, keyerror error ' ' | | Symmetric_difference (...) | Return the symmetric difference of the sets as a newSet. | | (I.E. all elements.IsIn exactly oneof theSets.)"Difference set: Return elements from two collections to a new collection >>> s1={1,2,3,4}>>> s2={1,2,3,4,5,6,7}>>> s3=s1.symmetric_ Difference (S2) >>> print (S3) {5, 6, 7} "| | Symmetric_difference_update (...) |Update ASetWith the symmetricDifferenceof itselfand another."Difference set: assigning different elements from two sets to the current collection >>> s1={1,2,3,4}>>> s2={1,2,3,4,5,6,7}>>> s1.symmetric_ Difference_update (S2) >>> print (S1) {5, 6, 7} "| |Union (...) |Return theUnionOfSetsAs aNewSet. | | (I.E. all elements.IsIn eitherSet.)"and set: Take two sets of the set to copy to a new collection >>> s1={1,2,3,4}>>> s2={4,5,6,7}>>> s3=s1.union (S2) >>> Print (S3) {1, 2, 3, 4, 5, 6, 7} "| |Update (...) | Update a set with the Union of itself and   others. "UPDATE: Add elements from the following collection to the current collection >>> s1={1,2,3,4," Yangge "}>>> s2={1,2,3,4,5,6,7}>>> s1.update (S2) >>> print (S1) {1, 2, 3, 4, 5, 6, 7, ' Yangge'} ' | | ---------------------------------------------------------------------- | Data and other  attributes defined here: | | __hash__ = nonenone
                                                                                                                                                                                                                                                                                                   

Python's set

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.