#__author: "Feng Lin"#DATE:2018/8/26#A collection is an unordered, non-repeating collection of data in which elements are hashed (immutable types), but the collection itself is not hashed (so the collection cannot be a dictionary key)#Collection note the following points in mind#1. Element de-weight#2. Relationship testing, intersection, set, and differenceSet1=set ({})#an immutable type of element in the collection, or an error will be#set2={1,2,3,{' name ': ' Alex '},[1,2]}#set add element, add, updateSet1={'wangjinxin','Penglei','Fangren','Liuchong','Lintianyi'}Print(Set1) Set1.add ("Goddess")Print(Set1)#update to increase fall-generation objectsSet1.update (Range (10))Print(Set1)#Random DeletePrint(Set1.pop ())#Delete by ElementSet1.remove (1)Print(Set1)#empties the collection, represented by an empty collection as set ()set1.clear ()Print(Set1)#del Set1#print (Set1)#Checkset2={'haha','Hihi','AA','xx','DD'} forIinchSet2:Print(i)#ask for intersectionset1={1,2,3}Print(Set1) Set2={4,5,6,7,8}Print(Set2) Set3=set1 &Set2Print(SET3)#Negation intersectionPrint(set1^Set2)#the two methods of seeking and collectingPrint(set1|Set2)Print(Set2.union (Set1))#Finding the difference setPrint(set1-Set2)Print(set2-Set1)#determine if a bool value is returned for a subset or supersetPrint(Set1 <Set2)Print(Set1 >Set2)#questions, go to the list below to re-li=[1,2,33,44,22,2,2,1,4,5,6,61,2,3]#turn a list into a set to go heavyli1=set (LI)Print(LI1)#re-converting the collection to a listLi=list (LI1)Print(LI)#Immutable Collection FrozensetS=frozenset ('Babsddaw')Print(S,type (s)) forIinchS:Print(i)
Basic Python Learning-collections