About the collation of the Python collection (set)

Source: Internet
Author: User

Collection

    1. Composition of different elements
    2. Disordered
    3. The elements in the collection must be immutable types (numbers, strings, tuples)

Definition of the collection:

s = {1,2,3,3,4}

s = {' Hello '}

Transformations can only be converted if the object is iterated

Set method

-add #将对象添加到集合中

S ={1,2,3,4,5,6}s.add ("s") S.add ('3') S.add (3) print (s) #结果: {1,2,3,4,5,6,'s','3'}

-clear

s ={1,2,3,4,5,6}s.clear () print (s) #结果:  Set()

-copy

s = {1,2,3,4,5,6= s.copy () print (S1) #结果: { 1 2 3 4 5 6}

-pop #随机删除

-remove #指定删除

s = {'SB', 1,2,3,4,5,6}s.pop ()Print(s)#Random DeleteS.remove ('SB')Print(s)#Specify Delete#s.remove (' helo ') #删除元素不存在, will errorS.discard ('SSBB')#delete element, not present or error-guaranteed#results: {2, 3, 4, 5, 6, ' SB '}#{2, 3, 4, 5, 6}

General methods for common items

Python_1 =['LCG','SZW','ZJW']linux_1= ['LCG','SZW']python_and_linux= [] forP_nameinchpython_1:ifP_nameinchlinux_1:python_and_linux.append (p_name)Print(Python_and_linux)#result: [' LCG ', ' szw ']
#in a collection of waysPython_1 =['LCG','SZW','ZJW']linux_1= ['LCG','SZW','SB']p_s= Set (python_1)#Convert to Collectionl_s =Set (linux_1)Print(p_s)
1 Print(l_s)2 Print(P_s.intersection (l_s))#ask for intersection3 Print(p_s&l_s)4 5 #Seek and set6 Print(P_s.union (l_s))7 Print(p_s|l_s)8 9 #Finding the difference setTen Print("Difference Set", p_s-l_s) One Print("Difference Set", l_s-p_s) A Print(P_s.difference (l_s)) -  -  the " "Results: - {' LCG ', ' zjw ', ' SZW '} - {' LCG ', ' sb ', ' SZW '} - {' LCG ', ' SZW '} + {' LCG ', ' SZW '} - {' LCG ', ' zjw ', ' sb ', ' SZW '} + {' LCG ', ' zjw ', ' sb ', ' SZW '} A differential set {' ZJW '} at difference set {' SB '} - {' ZJW '}" "
View Code
1Python_1 =['LCG','SZW','ZJW']2Linux_1 = ['LCG','SZW','SB']3p_s = Set (python_1)#Convert to Collection4l_s =Set (linux_1)5 #Cross complement set6 #Cross complement set7 Print(P_s.symmetric_difference (l_s))8 Print(p_s^l_s)9 Results:Ten{'SB','ZJW'} One{'SB','ZJW'}
Cross complement set
1-differnece_update2Python_1 =['LCG','SZW','ZJW']3Linux_1 = ['LCG','SZW','SB']4p_s =Set (python_1)5l_s =Set (linux_1)6 Print(p_s-l_s)7 Print(p_s)8P_s.difference_update (l_s)#update the differential set, the updated data is9 Print(p_s)Ten Results: One{'ZJW'} A{'LCG','ZJW','SZW'} -{'ZJW'}
Update differential Set
S1 = {1,2,3,4= {5,6,7}print(s1.isdisjoint (S2))# Determines if there is no intersection and returns true if there is no intersection  # Result: True
1 s1 = {2 s2}3print(s1.issubset (S2))# whether a subset is in the case 4 # Result: True
-judge S1 to be a subset of S2
1 Print (S2.issuperset (S1)) # whether it is a parent set 2 # Result: True
S1 = {$}
1 # go heavy, without regard to the order of the case 2 names = ["Alex","Alex",'Wupeiqi  ']3 names = list (set (names))4print( Names)5# results: [' Alex ', ' Wupeiqi ']
Frozenset () frozen set, i.e. immutable set

= {}s1.update (s2)# updates the data in the collection and can pass in an iterative object      print(S1)# Result: {1, 2, 3}

About the collation of the Python collection (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.