0. What is a collection?
A collection is an unordered, non-repeating combination of data, and its main functions are as follows:
Go to the weight, turn a list into a set, and then automatically go heavy.
Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
Set: A set of different elements that form a collection, which is the basic data type of Python.
collection element (set elements): the member that makes up the collection (non-repeatable)
A collection object is a set of unordered values that can be hashed (immutable, dictionary-like keys): A member of a collection can make a dictionary key
Collection classification: mutable sets, immutable collections
Mutable collection (SET): Elements that can be added and removed, not hashed, not as keys to a dictionary, or as elements of other collections
Immutable set (Frozenset): Contrary to the above
li=[[1,2],'a','b'TypeError: Unhashable type: ' list 'print(s)
A=set ('Alex Li a')#There are multiple aPrint(a) {' ','L','I','a','x','e'}#The collection will remove the duplicates and print them out in a disorderly order .b=['AAA','BBB','AAA']s=Set (b)Print(s) {'AAA','BBB'}#duplicates are removed from the list .
1. Create a Collection
Because the collection does not have its own syntax format, it can only be created through the collection's method set () and Frozenset (), or {} to create
S1 = set ('Alvin') s2= Frozenset ('yuan') # Immutable Collection S3={1,2,3}print(S1,type (S1)) #{' l ', ' V ', ' I ', ' a ', ' n '} <class ' Set ' >print(s2,type (S2)) #frozenset ({' n ', ' Y ', ' A ', ' u '}) <class ' Frozenset ' >
2. Access the collection
Because the collection itself is unordered, you cannot create an index or slice operation for the collection, but you can iterate through or use in and not to access or judge the collection element.
S1 = set ('Alvin')Print('a' inchS1)Print('b' inchS1)#s1[1] #TypeError: ' Set ' object does not support indexing forIinchS1:Print(i)# #True#False#v#N#L#I#a
3. Update the collection
You can use the following built-in methods to update:
Set.add (): Increased
Set.remove (): Delete
Set.update (): Change
Note: Only mutable collections can be updated:
#S1 = frozenset (' Alvin ')#s1.add (0) #AttributeError: ' Frozenset ' object has no attribute ' add 'S2=set ('Alvin') S2.add ('mm')#' mm ' will be deposited as a wholePrint(S2)#{' mm ', ' l ', ' n ', ' a ', ' I ', ' V '}S2.update ('HO')# add multiple elements, take the HO apart, add it as a single element, and if there are duplicates, it will go heavy .Print(S2)#{' mm ', ' l ', ' n ', ' a ', ' I ', ' H ', ' O ', ' V '}
S2.update ([12,'Alex','Eee','Alvin'])#inside the list, the element is a whole,Print(S2)#{' L ', ' n ', ' Alvin ', ' A ', ' I ', ' Alex ', ' V ', ' eee '}S2.remove ('L')#Specify Delete, when the value does not exist, will errorPrint(S2)#{' mm ', ' n ', ' a ', ' I ', ' H ', ' O ', ' V '}S2.discard ('VVVV')#Specify Delete, no error when the value is not presentS2.pop ()#random deletion of an elementS2.clear ()#emptying the collectiondelS2: Deleting the collection itself
4. Set type operator
1 in, not in: Determines whether an element is within the set
2 set equivalence and not equivalent (= =,! =)
3 subsets, Hyper-sets
4 Print (A.isdisjoint (b)), two sets returned as true without any association
Python Data Type-collection