Python basic data Type set

Source: Internet
Author: User

Set  is an unordered and non-repeating sequence
set is an unordered and non-repeating sequence set that does not allow duplicates of the set, set does not allow repeating sequences1. Create S=set ()#creating an empty collection can only be done in this ways={11,222,233,33}2. Convert L=[11,22,33,22]l= (11,22,33) L="123"s=set (L)3. Method S= {11,22,33,44}#add ElementS.add (55)Print(s)#clears the contents of a collections.clear ()Print(s)#s= {11,22,33,44}s1= {11,33,44}S3=s.difference (S1)#find presence in S, S1 not present, return to S3s.difference_update (S1)Print(s)#finds an element in the S collection that does not exist in the S1 and updates S. S.discard (11)Print(s)#removes the specified element, no error is presentret=s.intersection (S1)#take S, S1 intersection, return to RETs.intersection_update (S1)#the intersection and update Ss.isdisjoint (S1)#there is a intersection of true, no intersection is falseS.issubset (S1)#whether S1 is a sub-sequence of SS.issuperset (S1)#S is the parent sequence of S1S.pop ()#remove ElementS.remove (11)#removes the specified element without an errors.symmetric_difference (S1)#symmetric difference set, (s exists, S1 does not exist in S1 existence s non-existent collection)ret=s.symmetric_difference_update (S1)Print(ret)#symmetric difference set, (s exists, S1 does not exist S1 existence s does not exist collection) Updates.union (S1)#and setS.update ([11,212,3,66])#Update
Set

Set is an unordered and non-repeating sequence
Set does not allow duplicate collections, set does not allow repeating sequences
1. Create
S=set () #创建空集合只能用这种方法
S={11,222,233,33}
2. Conversion
L=[11,22,33,22]
L= (11,22,33)
L= "123"
S=set (L)

3. Methods
s= {11,22,33,44}
#增加元素
S.add (55)
Print (s)
#清除集合的内容
S.clear ()
Print (s)

#
s= {11,22,33,44}
s1= {11,33,44}
S3=s.difference (S1)
#查找s中存在, not present in the S1, returned to S3

S.difference_update (S1)
Print (s)
#查找s集合中存在, an element that does not exist in S1 and updates S.

S.discard (11)
Print (s)
# Remove the specified element, no error is present

ret = s.intersection (S1)
#取s, S1 intersection, return to RET
S.intersection_update (S1)
#取交集, and update s

S.isdisjoint (S1)
#有交集True, no intersection is false

S.issubset (S1)
#s1是否为s的子序列
S.issuperset (S1)
#s是否为s1的父序列

S.pop ()
#移除元素

S.remove (11)
# Remove the specified element, no error is present

S.symmetric_difference (S1)
#对称差集, (s exists, S1 does not exist S1 there is a non-existent collection of s)

ret= s.symmetric_difference_update (S1)
Print (ret)
#对称差集, (s exists, S1 does not exist S1 the presence of s does not exist in the collection) update

S.union (S1)
#并集

S.update ([11,212,3,66])
#更新



Python basic 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.