Usage of the Python set collection

Source: Internet
Author: User
Tags set set

Set Set: is a set of unordered, non-repeatable collections

Creation of 1.set

se={"ww3", 23432,"name"}# Create an empty collection Se1=set ()

2. Convert to a collection

li=[23,56,23,44]se=set (li)print(se)# output: {56, 44, 23} 

Common methods of 3.set

Add (self, *args, **kwargs): adds an element to the collection, and if the collection already has this element, it is not added for long
# element not added in collection case se={23,54,11,788}se.addprint(se)# output: {one, 788, wu, wu, si} # a situation in the collection where the element to be added is already in se={23,54,11,788}se.add ()print(se) # output: {one, 788, si, +}Clear (self, *args, **kwargs)

Clear (self, *args, **kwargs): Deletes all elements in the collection

se={23,54,11,788}se.clear ()print(se)# output: set ()

Difference (self, *args, **kwargs): compare two sets, find the element that the other does not have, and return a new collection

se={23,54,11,788}re={23,22,11,77}ge=se.difference (re)# Find all elements in se except for the same element as re  print(ge)# output: {788, si}

Difference_update (self, *args, **kwargs): compare a collection, find the element that the other does not have, and update yourself

se={23,54,11,788}re={23,54,11,77}se.difference_update (re)# Find all elements in se except for the same element as re  Print(se)
#输出: {788}

Discard (self, *args, **kwargs): removes an element from the collection, and nothing happens if the removed element is not in the collection

se={23,54,11,788}se.discard (all)print(se)# output: {11, 788, 54} 

Remove (self, *args, **kwargs): removes an element from the collection that must be a member of the collection if not, it will be an error .

Intersection (self, *args, **kwargs): returns the intersection of 2 sets as a new collection

se={23,54,11,788}re={23,44,55,66}ge=se.intersection (re)print(ge) # output: {%}

Intersection_update (self, *args, **kwargs): Find the same elements of two sets and update yourself

se={23,54,11,788}re={23,44,55,66}se.intersection_update (re)print(se)#  output: {%}

Pop (self, *args, **kwargs): arbitrarily removes an element from the collection and returns the removed element, or an error if the collection is empty

se={23,54,11,788}re=se.pop ()print(se)print(re) # output: {788, si, +}      

Update (self, *args, **kwargs): updates Another collection into its own collection

se={23,54,11,788}ge={23,324,567}se.update (ge)print(se)# output: { 788, 324 , wu, si, 567, one}

 

Usage of the Python set collection

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.