Python basics-set

Source: Internet
Author: User
The set is an unordered and non-repeating element set. set creation methods: se {11,22, 33} seset ([11,22, 33]) # call the set _ init _ method to create 2. common methods of set 1. addse {11,22, 33} se. add (44) print (se) & amp; gt; {33,11, 44,22} # execute because it is unordered... A set is an unordered and non-repeating element set.

1. create a set

Two methods:

Se = {11,22, 33}
Se = set ([11, 22, 33]) # create the _ init _ method of the called set

2. Common set methods

1. add

Se = {11,22, 33} se. add (44)
Print (se) => {33,11, 44,22} # The execution results are different because they are unordered, but 44 is indeed added to the original set.

2. remove

Se = {11,22, 33}
Se. remove (11)
Print (se) => {22, 33}
Se. remove (44) # An error is reported, indicating that the specified element cannot be found.

3. discard

Se = {11,22, 33} se. discard (11)
Print (se) => {22, 33} se. discard (44)
Print (se) = >{11,22, 33} # When the specified element cannot be found, it is not deleted and no error is reported.

4. pop

Se = {11,22, 33}
Se. pop () print (se) =>{11,22} # randomly outputs an element of the stack, and the execution results may be different. ret = se. pop () print (ret) =>{ 33} # print the result of the stack

5. difference

Se1 = {11, 22, 33, 44}
Se2 = {22, 33, 44, 55}
Print (se1.difference (se2) => 11 # print that se1 exists, but that does not exist in se2 print (se2.difference (se1) => 55 # print that se2 exists, but the element does not exist in se1.

6. difference_update

Se1 = {11,22, 33,44}
Se2 = {22,33, 44,55}
Se1.difference _ update (se2) print (se1) => 11 # overwrite the elements that exist in se1 and do not exist in se2 to se1 and update the set

7. intersection

Se1 = {11,22, 33,44}
Se2 = {22,33, 44,55}
Print (se1.intersection (se2) => {22, 33, 44} # se1, se2 intersection

8. intersection_update

Se1 = {11,22, 33,44}
Se2 = {22,33, 44,55}
Se1.intersection _ update (se2)
Print (se1) => {33,44, 22} # overwrite the intersection of se1 and se2 to the set of se1

9. union

Se1 = {11,22, 33,44}
Se2 = {22,33, 44,55}
Print (se1.union (se2) => {11,22, 33,44, 55} # se1, se2 Union

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.