Python Learning Notes (8th lesson)

Source: Internet
Author: User

In this section we will learn another Python data type: Collection (set)

1. Set (SET)

A collection in Python is an unordered data type with no duplicate elements and cannot be indexed to refer to elements in the collection

>>> basket = [' apple ', ' orange ', ' apple ', ' pear ', ' orange ', ' banana ']>>> set (basket) set ([' Orange ', ' pear ', ' apple ', ' banana ') 

Set1-set2 to get an element in the Set1 element that does not exist in Set2, returns a new collection. This function is the same as the Set1.difference (Set2) effect

>>> F=set ([1,2,3,4,5]) >>> c=set ([3,4,5,6]) >>> f.difference (c) Set ([1, 2] ) >>> list (f) [1, 2, 3, 4, 5]>>> Fcset ([1, 2])      

Set1|set2 gets the element in Set1 or Set2, returns a new collection

>>> F=set ([1,2,3,4,5]) >>> c=set ([3,4,5,6]) >>> f|  Cset ([1, 2, 3, 4, 5, 6])  

Set1&set2 gets the elements that exist in both Set1 and Set2, returning a new collection

>>> F=set ([1,2,3,4,5]) >>> c=set ([3,4,5,6]) >>> f&Cset ([3, 4, 5]) 

Set1^set2 gets the element that exists in Set1 or Set2, but does not include both in Set1 and Set2, and returns a new collection

>>> F=set ([1,2,3,4,5]) >>> c=set ([3,4,5,6]) >>> f^Cset ([1, 2, 6])  

2. Functions of the collection

Set.add (x) Adds an element x that is not added to the collection if X is already present in the collection

>>> F=set ([3,4,5,6])>>> f.add ('a')>>> Fset (['a', 3, 4, 5, 6])>>> f.add ('a ' )>>> fset (['a', 3, 4, 5, 6])

Set.clear () emptying the collection

>>> F=set ([3,4,5,6])>>> f.clear ()>>> fset ([])

Set.copy () Copy a collection

>>> F=set ([3,4,5,6])>>> s=f.copy ()>>> sset ([3, 4, 5, 6])

Set1.difference (Set2) Gets the element in the Set1 that does not exist in the Set2, and returns a new collection. Same as Set1-set2

>>> F=set ([3,4,5,6])>>> s=set ([1,2,3,4,5])>>> s.difference (f) Set ([1, 2])

Set1.difference_update (Set2) updates the Set1 so that Set1=set1-set2

>>> S=set ([1,2,3,4,5])>>> s.difference_update (f)>>> sset ([1 , 2])

Set.discard (x) Delete the element with the value x in the collection

>>> S=set ([1,2,3,4,5])>>> S.discard (3)>>> sset ([1, 2, 4, 5])

Set1.intersection (Set2) returns elements that exist in both Set1 and Set2, with the same effect as Set1&set2

>>> F=set ([3,4,5,6])>>> s=set ([1,2,3,4,5])>>> f.intersection (s) Set ([3, 4, 5])

Set1.intersection_update (set2) update Set1 so that Set1 equals Set1^set2

>>> F=set ([3,4,5,6])>>> s=set ([1,2,3,4,5])>>> F.intersection_ Update (s)>>> fset ([3, 4, 5])

Set1.isdisjoint (Set2) determines if Set1 and Set2 have the same element, if any, returns false, does not return true

>>> F=set ([3,4,5,6])>>> s=set ([1,2,3,4,5])>>> q=set ([1, 0]) >>> F.isdisjoint (s) False>>> f.isdisjoint (q) True

Set1.issubset (Set2) determines if Set1 is a subset of Set2, returns True if yes, otherwise false

>>> F=set ([up])>>> q=set ([3,4,5,6,7])>>> s=set ([1,2,3,4,5]) >>> f.issubset (q) False>>> f.issubset (s) True

Set1.issuperset (Set2) determines whether Set1 contains Set2, that is, whether Set2 is a subset of Set1, and if so, returns true, otherwise false

>>> F=set ([up])>>> s=set ([1,2,3,4,5])>>> f.issuperset (s) False >>> S.issuperset (f) True

Python Learning Notes (8th lesson)

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.