In addition to the common data types such as list, Tuple, and dict, Python also has a data type called set, which is characterized by the fact that the elements inside the collection are non-repeatable and the elements within the collection are Unordered. So generally the collection of two commonly used scenarios Are: 1. Go to weight (such as: list to go to weight); 2. Relationship testing (e.g., intersection, fetch and set, set of differences, etc.)
1) The collection contains a set of unordered objects, and you can use the Set () function to create the collection as follows
#!/usr/bin/env Python#-*-coding:utf-8-*-#创建一个数值集合set1 = set ([1,2,3,4]) #创建一个字符集合 note: from the output we will see that L only appeared once Set2 = Set (" helloworld! ") Print (' set1%s '% set1) print (' set2%s '% set2) ' run result set1 {1, 2, 3, 4}set2 {' d ', ' o ', ' e ', ' H ', ' l ', ' W ', ' r ', '! '} '
2) deduplication, which can be used in some special scenarios, to remove duplicate elements from the list
#!/usr/bin/env Python#-*-coding:utf-8-*-#列表去重比较简单list1 = [3,3,3,4,5,3]set1 = set (list1) print ("list1 de-reset set set:", Set1 #将如中后的集合在转化成一个新列表new_list = [i for i in Set1]print ("list1 list after de-listing:", New_list) ' run results list1 go back to the set set: {3, 4, 5} List of List1 to go back to: [3, 4, 5] "
3) relationship testing, which is the most basic operation of some set, such as collection intersection, set, take difference set, judge whether one set is a subset of another set or parent set, etc.
#!/usr/bin/env python# -*-coding:utf-8 -*-set1 = set ([1,2,3,4,5]) Set2 = set ([3,4,5,6,7]) #取交集set3 = set1.intersection (set2) # set3 = set1 & ste2 #取交集, Same as intersection () print (the intersection of Set1 and Set2 is: ", set3) #取并集set4 = set1.union (set2) #set4 = set1 | set2 #取并集, Same as the Union () print ("intersection of Set1 and set2:", set4) #取差集 this place to pay a little attention to prevent into the pit set5 = set1.difference (set2) #set5 = set1 - set2 #取差集 , Same as difference () print (difference between Set1 and Set2 is: ", set5) set6 = set2.difference (set1) Print (the difference between Set2 and Set1 is: ", set6) #对称差集 -----removes The common part of the two sets Set7 = set1.symmetric_difference (set2) #set7 = set1 ^ set2 #对称差集, with symmetric_difference () print ("remove The common part of two sets:", Set7) #判断是否是set1是否是set2的子集flag1 = set1.issubset (set2) print ("print (determines If Set1 is a subset of set2):", Flag1) # Determines whether Set1 is a parent set of Set2 flag2 =&Nbsp;set1.issuperset (set2) Print ("determines Whether Set1 is the parent set of set2:", Flag2) ' runs the result as Follows: Set1 and Set2 intersect: {3, 4, 5} The intersection of Set1 and Set2 is: the difference between {1, 2, 3, 4, 5, 6, 7}set1 and Set2 is: {1, 2} The difference between Set2 and Set1 is: {6, 7} removes a common part of two sets: {1, 2, 6, 7}print (determines if Set1 is a subset of set2): False to determine whether Set1 is the parent set of Set2: false "
4) some other basic operations of the collection
#!/usr/bin/env Python#-*-coding:utf-8-*-#输出集合中的元素 # Note collections are different from lists and tuples, so the collection is unordered, so it is not possible to index the value of an element by a number Set1 = Set ([1,2,3,4]) For i in Set1:print (i) #向集合中添加一个元素set1. add (5) print ("after Adding an element 5 to a collection:", set1) #删除一个元素set1. remove (1) print ("after removing element 1 from the collection:", Set1) #计算集合的长度l = len (set1) print ("the length of the collection is:", L) #判断某个元素是否在集合内flag1 = 2 in Set1print ("determine if element 2 is within the collection:", Flag1) # Whether an element is not within the collection Flag2 = 3 Not in Set1print ("determines whether element 3 is not within the collection:", flag2) #对集合进行一次浅复制set2 = set1.copy () print ("a Shallow copy of the collection:", Set2) ' The results are as follows: 1234 after adding an element 5 to the collection: {1, 2, 3, 4, 5} After removing element 1 from the collection: {2, 3, 4, 5} The length of the collection Is: 4 determines whether element 2 is within the collection: true to determine if element 3 is not in the collection: {2, 3, 4, 5} "
These are examples of some of the more common operations of set (set), which are not illustrated here in one by one for some other operations of the Collection.
The basic operations of collections (set) in Python and some common uses