0. What is a collection?
A set is an unordered, non-repeating combination of data , and its main functions are as follows:
Deduplication, turning a list into a collection, it will automatically deduplicate
Relationship test, test the intersection, difference, union and other relationships before two sets of data
Set (set): Grouping different elements together to form a set is the basic data type of python.
set elements: the members that make up the set ( non-repeatable )
A collection object is an unordered set of hashable (immutable, dictionary-like keys) values : members of the collection can act as dictionary keys
Collection classification: mutable collection, immutable collection
Mutable set (set): elements can be added and removed, non-hashable, cannot be used as a dictionary key, and cannot be used as elements of other sets
Immutable collections (frozenset): the exact opposite of the above
li = [[1,2], ' a ' , ' b ' ] s =set(li) # TypeError: unhashable type: 'list' print(s)
a=set( ' alex li a ' ) #There are multiple a print (a) { ' ' , ' l ' , ' i ' , ' a ' , ' x ' , ' e ' } #The set will remove duplicates and print them out of order b =[ ' aaa ' , ' bbb ' , ' aaa ' ] s=set(b) print(s) { ' aaa ' , ' bbb ' } #If there are duplicates in the list, the duplicates will be removed
1. Create a collection
Since the collection does not have its own syntax format, it can only be created through the methods set() and frozenset() of the collection , or {} to create
s1 = set('alvin') s2 = frozenset( ' yuan ' ) #immutable set 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
Since the collection itself is unordered, indexing or slicing operations cannot be created for the collection, only loop traversal or use in, not in to access or judge the elements of the collection.
s1 = set('alvin') print('a' in s1) print('b' in s1) #s1[1] #TypeError: 'set' object does not support indexing for i in s1: print(i) # # True # False # v # n # l # i # a
3. Update the collection
Updates can be made using the following built-in methods:
set.add(): add
set.remove():删
set.update():改
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 stored as a whole print (s2) # { 'mm' , 'l', 'n', 'a', 'i', 'v'} s2. update( ' HO ' ) #Add multiple elements, split the HO, and add it as a single element. If there are duplicates, it will be de-duplicated print (s2) # {'mm', 'l', 'n', ' a', 'i', 'H', 'O' , 'v'}
s2.update([12, ' alex ' , ' eee ' , ' alvin ' ]) #In the list, the element is a whole, print (s2) # {'l', 'n', ' alvin' , 'a' , 12 , 'i', 'alex' , 'v', 'eee' } s2.remove( ' l ' ) #Specify delete, when the value does not exist, an error will be reported print (s2) # {'mm', ' n', 'a', 'i', 'H', 'O', 'v'} s2.discard( 'vvvv ' ) #Specify delete, when the value does not exist, no error will be reported s2.pop() #Delete an element randomly s2.clear () #Empty the collection del s2 : delete the collection itself
4. Collection type operators
1 in , not in: determine whether an element is in the set
2 Set equivalence and inequality (==, !=)
3 Subsets, Supersets
4 print( a.isdisjoint (b)), returns true if the two sets are not related in any way