Python-set collection methods

Source: Internet
Author: User
Tags union of sets
This article details the python-set collection class methods. s1 = set ([2.5, 'Tom ', 'Tony',]) returns, 2.5, 'Tom ', 'Tony', 77,} (note: The Returned result is not a dictionary, but it just tells you that the collection contains these elements, therefore, the order of the returned result elements may be different)

S2 = set ([, 'Tom ', 'Tony', 66,]) returns {, 'Tom ', 'Tony ', 55,66} (note: The Returned result is not a dictionary, but tells you that the collection contains these elements, so the order of the returned result elements may be different each time)

Add (...)
Add an element to a set.
This has no effect if the element is already present. (if the element already exists, the addition is invalid. It means that set is a set with no repeated elements .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S1.add ('Jimi ')

Print (s1)

Result: {33, 'Tony ', 2.5, 'Jimi', 11, 44, 77, 22, 'Tom'} (note: set is an unordered set, the sequence of results may be different for each run .)

Difference (...)
Return the difference of two or more sets as a new set. (calculate the primary difference set and generate a new set)
(I. e. all elements that are in this set but not the others .) (Note: s1.difference (s2) indicates finding elements in s1 but not in s2 and generating a new set. S2.difference (s1) indicates finding elements in s2 but not in s1 and generating a new set .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 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 this set. (Remove all elements from this set. Note: s1.difference _ update (s2) indicates removing elements that are common to both s1 and retaining elements that are not common to S1. instead of generating a new list, S1. S2.difference _ update (s1) indicates removing the elements that are common to both s2 and retaining the elements that are different from each other. instead of generating a new list, S2 .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S1.difference _ update (s2)

Print (s1)

Result: {2.5, 77}

Discard (...)
Remove an element from a set if it is a member. (Remove a member from the set (element ))
If the element is not a member, do nothing. (If this member is not in the set, no operation is performed and no error is reported. this is different from Remove .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S1.discard (2.5)

Result: {33, 'Tom ', 11, 44, 77, 'Tony', 22}

Intersection (...)
Return the intersection of two sets as a new set. (generate the intersection of the two sets and generate a new list)
(I. e. all elements that are in both sets .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S5 = s1.intersection (s2)

S6 = s2.intersection (s1)

Print (s5)

Print (s6)

Result: {33, 'Tom ', 11, 44, 22, 'Tony '}

{33, 11, 44, 'Tony ', 'Tom', 22}

Intersection_update (...)
Update a set with the intersection of itself and another. (function and intersection (...) yes, but s1.intersection (s2) modifies the original set s1 during execution and does not generate a new set)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S1.intersection (s2)

S2.intersection (s1)

Print (s1)

Print (s2)

Result: {33, 'Tom ', 11, 44, 22, 'Tony '}

{33, 11, 44, 'Tony ', 'Tom', 22}

Isdisjoint (...)
Return True if two sets have a null intersection. (checks whether the two sets have an intersection. if yes, False is returned. if no intersection exists, True is returned)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([500, 50,])

Print (s1.isdisjoint (s2 ))

Result: True.

Issubset (...)
Report whether another set contains this set. (Judge whether all elements in a set are in another set. s1.issubset (s2) indicates that each element in s1 is in S2. note the difference between s1.issuperset (s2) and s1.issuperset. )

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

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

S3 = ([, 'Tom ', 'Tony', 66])

S1.issubset (s2)

S1.issubset (s3)

Print (s1.issubset (s2 ))

Print (s1.issubset (s3 ))

Result: True.

False

Issuperset (...)
Report whether this set contains another set. (Judge whether all elements in a set are in another set. s1.issubset (s2) indicates that each element in s2 is in S1. note the difference between s1.issubset (s2) and s1.issubset. )

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

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

S3 = ([, 'Tom ', 'Tony', 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. (delete an element randomly)
Raises KeyError if the set is empty. (if the set is empty, KeyError is prompted when pop is executed)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S1.pop ()

Result: {33, 'Tom ', 2.5, 11, 44, 77, 22}

Remove (...)
Remove an element from a set; it must be a member. (Remove the member (element) in the set. this element must be in this set)
If the element is not a member, raise a KeyError. (If this member is not found in the collection, a keyError is displayed, which is different from discard .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S1.remove (2.5)

S1.remove ('Jimi ')

Result: {33, 'Tom ', 'Tony', 11, 44, 77, 22}

KeyError: 'Jimi'

Symmetric_difference (...)
Return the specified Ric difference of two sets as a new set. (generate a new list, which is a set of non-repeating elements in the two lists, s1.20.ric _ difference (s2) it indicates a set of elements in s1 that are not in s2 and that in s2 that are not in s1)

(I. e. all elements that are in exactly one of the sets .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S4 = s2.1_ric _ difference (s1)

Print (s4)

Result: {2.5, 66, 77, 55}

Symmetric_difference_update (...)
Update a set with the specified Ric difference of itself and another. (modify a list. There are two sets of non-repeated elements in the list, s1.20.ric _ difference (s2) it indicates a set of elements in s1 that are not in s2 and that in s2 that are not in s1)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S1.symmetric _ difference_update (s2)

Print (s1)

Result: {2.5, 66, 77, 55}

Union (...)
Return the union of sets as a new set. (generate a new set. The Changed list is a set of members (elements) in two lists. s1.union (s2) indicates a new set containing all elements of s1 and s2)
(I. e. all elements that are in either set .)

Example: s1 = set ([2.5, 'Tom ', 'Tony',])

S2 = set ([, 'Tom ', 'Tony', 66,])

S3 = s1.union (s2)

Print (s3)

Result: {33, 2.5, 66, 'Tom ', 11, 44, 77, 55, 22, 'Tony '}

Update (...)
Update a set with the union of itself and others. (update a set to another set. s1.update (s2) indicates that all elements in s2 are put into s1 to update and modify s1)

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

S2 = set ([, 'Tom ', 'Tony', 66,])

S1.update (s2)

Print (s1)

Result: {'Tony ', 33, 66,100, 'Tom', 11, 44, 50, 22, 55}

The above is a detailed introduction to the python-set collection method. For more information, see other related articles in the first PHP community!

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.