Set Operations,

Source: Internet
Author: User

Set Operations,

Set definition:

  • Different Elements
  • Unordered
  • The element in the set must be of an unchangeable type.

 

Create a set

S = set ("hello") print (s) or s = {1, 2, 3, 3} print (s)

 

 

Set built-in Methods

1. add

# Add element s = {1, 2, 4, 5} s. add ("reese") print (s) # output {1, 2, 3, 4, 5, 'reese '}

 

 

2. clear

# Clear s = {1, 2, 4, 5} s. clear () print (s) # output result: set ()

 

 

3. pop

# Randomly Delete s = {"re", 3, 4, 5, 6, 8} s. pop () print (s) # output result: {4, 5, 6, 8, 'Re '}

 

 

4. remove (delete. An error is returned if the deleted element does not exist)

# Specify to delete s = {1, 2, 4, 5, 7, "neo"} s. remove ("neo") print (s) # output result: {1, 2, 3, 4, 5, 7}

 

 

5. discard

# Specify the deletion. If the deletion element does not exist, no error will be reported for s1 = {1, 2, 3, 4, 5, 7, "neo"} s1.discard ("neo") print (s1) s2 = {1, 2, 3, 4, 5, 7, "neo"} s2.discard ("neoll") print (s2) # output result: {1, 2, 3, 4, 5, 7} {1, 2, 3, 4, 5, 7, 'neo '}

 

 

6. intersection

S1 = {12, 34, 56,123,890} s2 = {354, 45, 78, 34,123, 90} print (s1 & s2) # print the intersection (s1.intersection (s2 )) # output result: {34,123} {34,123}

 

 

7. union

S1 = {"reese", 123,567, 89} s2 = {123, 89, 45, "neo"} # print (s1 | s2) print (s1.union (s2 )) # output result: {'neo ', 'reese', 45,567, 89,123} {'neo ', 'reese', 45,567, 89,123}

 

 

 

8. difference

S1 = {"reese", 123,567, 89} s2 = {123, 89, 45, "neo"} # difference set print (s1-s2) print (s1.difference (s2 )) # output result: {'reese ', 567} {'reese', 567}

 

 

 

9. effecric_difference

S1 = {"reese", 123,567, 89} s2 = {123, 89, 45, "neo"} # print ("crossover set", s1 ^ s2) print ("crossover set", s1.equalric _ difference (s2) # output result: crossover set {'reese ', 45, 'neo', 567} crossover set {'reese ', 45, 'neo, 567}

 

 

 

10. isdisjoint

S1 = {, 3} s2 = {, 6} s3 = {, 0} print (s1.isdisjoint (s2) print (s1.isdisjoint (s3) # output result: TrueFalse

 

 

11. issubset

S1 = {1, 2, 3} s2 = {1, 2, 3} print (s1.issubset (s2) # s1 is a subset of s2 # output result: True

 

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.