Python set relational operation intersection, Difference DAY14

Source: Internet
Author: User

Go to weight and create a collection (unordered)
Python_1 = ['1','2','3']linux_1= ['1','2'] #python_and_linux_1= []# forP_nameinchpython_1:#ifP_nameinchlinux_1:# python_and_linux_1.append (p_name) #print (python_and_linux_1) p_s=Set(python_1) print (p_s)

Ask for Intersection

Python_1 = ['1','2','3']linux_1= ['1','2'] #python_and_linux_1= []# forP_nameinchpython_1:#ifP_nameinchlinux_1:# python_and_linux_1.append (p_name) #print (python_and_linux_1) p_s=Set(python_1) l_s=Set(linux_1) print (p_s,l_s) print (P_s.intersection (l_s)) print (p_s&l_s) #同上述函数

Seek and set

Python_1 = ['1','2','3']linux_1= ['1','2','4'] #python_and_linux_1= []# forP_nameinchpython_1:#ifP_nameinchlinux_1:# python_and_linux_1.append (p_name) #print (python_and_linux_1) p_s=Set(python_1) l_s=Set(linux_1) print (p_s,l_s) print (P_s.union (l_s)) print (p_s|l_s)

Finding the difference set

Python_1 = ['1','2','3']linux_1= ['1','2','4'] #python_and_linux_1= []# forP_nameinchpython_1:#ifP_nameinchlinux_1:# python_and_linux_1.append (p_name) #print (python_and_linux_1) p_s=Set(python_1) l_s=Set(linux_1) print (p_s,l_s)
Print (P_s.difference (l_s))

print (p_s-l_s)

Set other methods

Cross complement set-intersection

Python_1 = ['1','2','3']linux_1= ['1','2','4'] #python_and_linux_1= []# forP_nameinchpython_1:#ifP_nameinchlinux_1:# python_and_linux_1.append (p_name) #print (python_and_linux_1) p_s=Set(python_1) l_s=Set(linux_1) print (p_s,l_s) print (P_s.symmetric_difference (l_s)) print (p_s^l_s)

Difference_update

p_s=p_s-l_s

==p_s.difference_update (l_s)

Similarly intersection_update

Isdisjoint Two collections no common elements

Returns a Boolean value

Issubset,s1 contained in S2, subset

Equivalent to S1<S2

Returns a Boolean value

 python_1 = [ " 1  , "  2  , "  3   " ]linux_1  = [ " 1  , "  2  , "  4   " ]p_s  = set   ( python_1) l_s  = set   (linux_1) print (p_s,l_s ) Print (P_s.issubset (l_s))  

Issuperset, Parent set

Update updates element, add only adds one element

s1={1,2}s2={1,2,3,4}s1.update (s2) print (S1) 

Python set relational operation intersection, Difference DAY14

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.