A detailed introduction to Python-set Collection class methods

Source: Internet
Author: User
Tags union of sets
S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,]) returned {11,22,33,44, ' Tom ', ' Tony ', 77,2.5} (Note: The return is not a dictionary, Just tell you that this collection contains these elements, so the order of the result elements returned each time may be different.

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,]) returned {11,22,33,44, ' Tom ', ' Tony ', 55,66} (Note: The return is not a dictionary, Just tell you that this collection contains these elements, so the order of the result elements returned each time may be different.

Add (...)
Add an element to a set.
This have no effect if the element is already present. (if the element already exists, the addition is not valid.) This means that set is a collection of elements that have no duplicates. )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S1.add (' Jimi ')

Print (S1)

Results: {, ' Tony ', 2.5, ' Jimi ', One, A, a, a, a, ' Tom '} (Note: Set is an unordered collection and may be different in the order of the results of each run.) )

Difference (...)
Return the difference of one or more sets as a new set. (The main difference is set, and an additional collection is generated)
(i.e. all elements, that is in this set and not the others.) (Note: s1.difference (s2) means finding elements in S1 that are not in the S2 and generating a new collection. S2.difference (S1) indicates that there are elements in S2 that are not in the S1 and that a new collection is generated. )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S3=s1.difference (S2)

S4=s2.difference (S1)

Print (S3)

Print (S4)

Result: {2.5, 77}

{66, 55}

Difference_update (...)
Remove all elements of another set from the this set. (Removes all elements of another group from this collection.)    Note: s1.difference_update (s2) represents the removal of elements that are common in S1, preserving elements that are not common, and modifying the S1 rather than generating a new list. S2.difference_update (S1) represents the removal of elements that are common in S2, preserving elements that are not common, and modifying the S2 rather than generating a new list. )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S1.difference_update (S2)

Print (S1)

Result: {2.5, 77}

Discard (...)
Remove an element from a set if it is a member. (Removes members (elements) from the collection)
If the element is not a member, does nothing. (if there is no such member in the collection, no action is made, and no error is found, which differs from remove.) )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S1.discard (2.5)

Results: {$, ' Tom ', one, one, one, one, ' Tony ', 22}

Intersection (...)
Return the intersection of two sets as a new set. (generates the intersection of two collections and generates a new list)
(i.e. all elements is in both sets.)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S5=s1.intersection (S2)

S6=s2.intersection (S1)

Print (S5)

Print (S6)

Results: {$, ' Tom ', one, one, one, ' Tony '}

{One, one, one, ' Tony ', ' Tom ', 22}

Intersection_update (...)
Update a set with the intersection of itself and another (features and intersection (...) is the same, but s1.intersection (S2) is performing a change to the original set S1, and does not generate a new collection)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S1.intersection (S2)

S2.intersection (S1)

Print (S1)

Print (s2)

Results: {$, ' Tom ', one, one, one, ' Tony '}

{One, one, one, ' Tony ', ' Tom ', 22}

Isdisjoint (...)
return TRUE if two sets have a null intersection. (Determines whether two sets have intersections, if any, returns false if not, returns True if not)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([100, 50,500,])

Print (S1.isdisjoint (S2))

Result: True

Issubset (...)
Report whether another set contains the this set. (To determine whether all elements in a collection are in another collection, S1.issubset (S2) represents that each element in S1 is in S2, note the S1.issuperset (S2) difference. )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,])

S3= ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66])

S1.issubset (S2)

S1.issubset (S3)

Print (S1.issubset (S2))

Print (S1.issubset (S3))

Result: True

False

Issuperset (...)
Report whether the This set contains another set. (To determine whether all elements in a collection are in another collection, S1.issubset (S2) indicates that each element in S2 is in S1, note with S1.issubset ( S2) The difference. )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,])

S3= ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66])

S1.issuperset (S2)

S1.issuperset (S3)

Print (S1.issuperset (S2))

Print (S1.issuperset (S3))

Result: True

False

Pop (...)
Remove and return an arbitrary set element. (Random deletion of an element)
Raises keyerror if the set is empty. (If the collection is empty, Keyerror is alerted when the pop is executed)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S1.pop ()

Results: {$, ' Tom ', 2.5, 11, 44, 77, 22}

Remove (...)
Remove an element from a set; It must be a member. (Removes the member (element) from the collection, which must be in this collection)
If the element is not a member, raise a keyerror. (if there is no member in the collection, you will be prompted for Keyerror, which is different from discard.) )

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S1.remove (2.5)

S1.remove (' Jimi ')

Results: {$, ' Tom ', ' Tony ', 11, 44, 77, 22}

Keyerror: ' Jimi '

Symmetric_difference (...)
Return the symmetric difference of both sets as a new set. (Generate a new list, which is a collection of non-repeating elements in two lists, s1.symmetric_difference (S2) indicates that there are no elements in S2 in S1 and a collection of elements in S2 that are not in S1)

(i.e. all elements is in exactly one of the sets.)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S4=s2.symmetric_difference (S1)

Print (S4)

Results: {2.5, 66, 77, 55}

Symmetric_difference_update (...)
Update a set with the symmetric difference of itself and another. (Modify a list, the collection of non-repeating elements in two lists, s1.symmetric_difference (S2) Represents a collection of elements in the S1 that are not in the S2 and elements in the S2 that are not in the S1)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S1.symmetric_difference_update (S2)

Print (S1)

Results: {2.5, 66, 77, 55}

Union (...)
Return the union of sets as a new set. (Generates a collection of two lists, so a collection of members (elements), s1.union (S2) represents a new collection containing all the elements of S1,S2)
(i.e. all elements is in either set.)

For example: S1=set ([11,22,33,44, ' Tom ', ' Tony ', 11,77,2.5,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S3=s1.union (S2)

Print (S3)

Results: {2.5, $, ' Tom ', one, one, one, one, one, ' Tony '}

Update (...)
Update a set with the Union of itself and others. (Update a collection to another collection, s1.update (S2) to put all the elements in S2 into S1, complete the update modification to S1)

Example: S1=set ([100, 50,])

S2=set ([11,22,33,44, ' Tom ', ' Tony ', 11,55,66,])

S1.update (S2)

Print (S1)

Results: {' Tony ', ' Max ', ' Max ', ' Tom ', ' one ', ' + ', ' + ', ' + '

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.