Python Road (iv)-set collection

Source: Internet
Author: User
Tags set set

Set set set is an unordered and non-repeating collection of elements
Advantages: Fast access to solve repetitive problems
L1 = [1,2,34,5,6,7,4,3,3= set (L1)  #可以以列表元祖等作为参数传进来, the set set will automatically convert Cheng Yuanju and go back to
#输出结果:
{1, 2, 34, 4, 5, 6, 7, 3}

Various methods: Add,clear,..... Wait a minute

Add Merge Collection
L1 = [1,2,34,5,6,7,4,3,3= [23,3,4,5,100,2,3= set (L1)  # auto-convert and go to S2.add (  )print(S2)
Cear
L1 = [1,2,34,5,6,7,4,3,3= [23,3,4,5,100,2,3= set (L1)  # auto-convert and go to S2.add (  ) s2.clear ()print(S2)
Copy
L1 = [1,2,34,5,6,7,4,3,3= set (L1)  # automatic conversion and de-weight s3 = s2.copy ()Print  (S3)# output:{1, 2, 34, 4, 5, 6, 7, 3}
Difference  seeking difference set
L1 = [1,2,34,5,6,7,4,3,3= set (L1)  # automatic conversion and de-weight s2 = set ([12,3465,75,32,2#  The elements in the Loop S2 are compared to S1, and if there is no S1  , the elements in the loop S1 are compared with S2, and print prints (S1) are printed if there is no S3  Print(S4)# output:{4, 3465, 1, {34, 3, 5}
The difference between difference_update and difference is that it will update the original collection
L1 = [1,2,34,5,6,7,4,3,3= set (L1)  # automatic conversion and de-weight s2 = set ([12,3465,75,32,2]) S2.difference_update (S1)print(s2)# output:{32, 3465, 75, 12}
The Discard  parameter is a string or a number, and if the parameter is within the collection, it is deleted, and if it is not, nothing is done
#清除的元素不存在, do not take any action
L1 = [1,2,34,5,6,7,4,3,3,'a','AB']s1= Set (L1)#automatic conversion and de-weightS1.discard ('ABC')Print(S1)#Output Result:{1, 2, 34, 4, 5, 6, 7, 3,'a','AB'} #参数存集合内, the element is cleared L1= [1,2,34,5,6,7,4,3,3,'a','AB']s1= Set (L1)#automatic conversion and de-weightS1.discard ('AB')Print(S1)#Output Result:{1, 2, 34, 4, 5, 6, 7, 3,'a'}
Intersection the intersection between two combinations
L1 = [1,2,34,5,6,7,4,3,3,'a','ab'= set (L1)  # automatic conversion and de -s2 = set ([1,2,23,3,3,5,2= s2.intersection (S1)print(S3)  # output results:
{1, 2, 3, 5}
The Intersection_update function is the same as the intersection, except that it updates the original collection
L1 = [1,2,34,5,6,7,4,3,3,'a','ab'= set (L1)  # automatic conversion and de -s2 = set ([1,2,23,3,3,5,2]) s2.intersection_update (S1)print(S2) # Output Results {1, 2, 3, 5}
Isdisjoint returns True if there is no intersection, or false if not
L1 = [1,2,34,5,6,7,4,3,3,'a','AB']s1= Set (L1)#automatic conversion and de-weightS2 = Set ([1,2,23,3,3,5,2]) S3=s2.isdisjoint (S1)Print(S3)#Output ResultsFalsel1= [1,2,34,5,6,7,4,3,3,'a','AB']s1= Set (L1)#automatic conversion and de-weightS2 = Set ([111,222]) S3=s2.isdisjoint (S1)Print(S3)#Output ResultsTrue
If Issubset is  a subset, yes returns true, otherwise false
L1 = [1,2,34,5,6,7,4,3,3,'a','ab'= set (L1)  # automatic conversion and de -s2 = set ([111,222= s2.issubset (S1)print(S3)#  Output Result:False
Issuperset is the parent set, YES returns True, otherwise false
L1 = [1,2,34,5,6,7,4,3,3,'a','ab'= set (L1)  # automatic conversion and de -s2 = set ([111,222= s2.issuperset (S1)print(S3)#  Output Result:False
Pop randomly deletes an element and returns
L1 = [1,2,34,5,6,7,4,3,3,'a','ab','ds '  = set (L1)  # automatic conversion and de -s2 = s1.pop (2)print(S2) # Random result of output result
Remove removes a specified element and does not return
L1 = ['a','AB','DS']s1= Set (L1)#automatic conversion and de-weightS1.remove ('a')Print(S1)#Output Results{'AB','DS'}
Symmetric_difference symmetric difference, which refers to merging two sets and then finding the difference set
L1 = ['a','AB','DS']s1= Set (L1)#automatic conversion and de-weightS2 = Set ([1,2,3,4,'C','D','a']) S3=s1.symmetric_difference (S2) S4=s2.symmetric_difference (S1)Print(S3)Print(S4)#two times the result is the same.{1, 2, 3, 4,'D','C','AB','DS'}{1, 2, 3, 4,'D','DS','C','AB'}
Symmetric_difference_update is the same as the Symmetric_difference function, except that it updates the original collection
Update updates an element
L1 = ['a','AB','DS']s1= Set (L1)#automatic conversion and de-weightS1.update ('CD')Print(S1)#Output Result:{'D','AB','DS','a','C'}




Python Road (iv)-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.