Python data type-set

Source: Internet
Author: User

Set is an unordered and non-repeating collection of elements


1. Create a Collection

>>> S1 = set () >>> s1.add (' a ') >>> s1{' a '}>>> s1.add (' a ') >>> s1{' a '}> >> S1 = set ([' A ', ' B ', ' C ']) >>> s1{' B ', ' A ', ' C '}


2. Clear elements

>>> s1{' A '}>>> s1.clear () >>> S1set ()


3. Difference Set

>>> s2 = set ([' A ', ' B ', ' C ']) >>> s3 = S2.difference ([' A ', ' B ']) >>> print (s2) {' B ', ' A ', ' C '}> >> Print (S3) {' c '}>>> s2 = set ([' A ', ' B ', ' C ']) >>> s3 = S2.difference_update ([' A ', ' B ']) >> > Print (s2) {' C '}>>> print (s3) none>>> s2 = set ([' A ', ' B ', ' C ']) >>> s3 = S2.difference_ Update ([' A ', ' d ']) >>> print (s2) {' B ', ' C '}>>> print (S3) None




5. Delete

>>> s2 = set ([' A ', ' B ', ' C ']) >>> s3 = S2.discard (' a ') >>> print (s2) {' B ', ' C '}


6. Intersection

>>> S1 = set ([' B ', ' C ', ' d ']) >>> s2 = s1.intersection ([' C ', ' d ']) >>> print (S1) {' B ', ' d ', ' C '} >>> print (s2) {' d ', ' C '}>>> s1 = set ([' B ', ' C ', ' d ']) >>> s2 = s1.intersection_update ([' C ', ' d '] ) >>> Print (S1) {' d ', ' C '}>>> print (s2) None


7. Is the intersection

If there is no intersection, return true>>> S1 = set ([' B ', ' C ', ' d ']) >>> s2 = S1.isdisjoint ([' C ', ' d ']) >>> print (s2) false>>> s2 = s1.isdisjoint ([' E ', ' f ']) >>> print (s2) True


8. Whether it is a subset

>>> S1 = set ([' B ']) >>> s2 = S1.issubset ([' A ', ' B ', ' C ']) >>> print (s2) true>>> s2 = S1.issubset ([' A ', ' e ', ' C ']) >>> print (s2) False


9. is the parent set

>>> S1 = set ([' B ', ' C ', ' d ']) >>> s2 = S1.issuperset ([' B ']) >>> print (s2) True


10. Delete

>>> S1 = set ([' B ', ' C ', ' d ']) >>> re = S1.pop () >>> print (S1) {' d ', ' C '}>>> print (re) b >>> S1 = set ([' B ', ' C ', ' d ']) >>> re = s1.remove (' b ') >>> print (S1) {' d ', ' C '}>>> print ( RE) None


11. Symmetric difference Set

>>> S1 = set ([' B ', ' C ', ' d ']) >>> s1.symmetric_difference ([' d ', ' e ']) {' C ', ' B ', ' E '}>>> print ( S1) {' C ', ' B ', ' d '}>>> s1 = set ([' B ', ' C ', ' d ']) >>> s1.symmetric_difference_update ([' d ', ' e ']) >> > Print (S1) {' C ', ' B ', ' E '}


12. and set

>>> S1 = set ([' B ', ' C ', ' d ']) >>> s1.union ([' d ', ' e ']) {' C ', ' B ', ' e ', ' d '}>>> s1 = set ([' B ', ' C ', ') ' d ']) >>> s1.update ([' d ', ' e ']) >>> print (S1) {' C ', ' B ', ' e ', ' d '}












This article is from the "guoxianqi2016" blog, make sure to keep this source http://guoxianqi2016.blog.51cto.com/11407620/1765731

Python data type-set

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.