Review:
strings, like tuples, can only be read and cannot be written. Lists and dictionaries can nest anything, lists can be nested
L = List ("Hello") # L = [' h ', ' e ', ' l ', ' l ', ' O ']
3. Collection Operations
A set is a combination of unordered and non-repeating arrays, the main role
- 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
Common operations:
1 #Author: Ling Xianpeng2s = set ([3,4,5,6,7])#Create a number set {3, 4, 5, 6, 7}3W = set ([2,3,4])4t = Set ("Hello")#iterate through each character in the string, tearing it apart into a single character {' H ', ' e ', ' l ', ' o '}5My_set = Set ("Hello",))#Create a collection containing a single element, {' Hello '} with commas, no commas, or it will be disassembled6 Print(My_set)7My_set = Set ("haha","Wode","en"))#Create a collection containing multiple elements {' en ', ' haha ', ' wode '}8 Print(My_set)9 Print(S&W)#Find intersection {3, 4}Ten Print(S | w)#seek the set {2, 3, 4, 5, 6, 7} One Print(S-W)#differential set, minus the same element {5, 6, 7} A Print(w-s)#{2} - Print(s ^ w)#set minus intersection -T.add ('W') the Print(t)#{' O ', ' e ', ' l ', ' h ', ' W '} - Print(T.add ('W'))#none############# -T.update ([12,23,34]) - Print(t)#add multiple items {, ' O ', ' w ', ' n ', ' h ', ' l ', Max, ' e '} + #let's try typing the string . -T.update ("Wode")#when a string is split into a single character and then added to the collection one after the other, there are duplicates that are ignored. , {' W ', ' H ',,, ' d ', ' l ', ' n ', ' e ', ' O '} + Print(t) AT.update ({"Wode"})#Add a string to the collection, {' H ',,, ' e ', ' o ', ' wode ', ' n ', ' l ', ' d ', Max, ' W '} at Print(t) -T.remove (34)#An error occurs if the element to be removed does not exist - Print(t)#Remove the element, {' W ', ' e ', ' o ', ' l ', +, ' H ', Max} -T.discard ('hahaha')#does not exist no error occurs - Print(t)#{' W ', N, ' e ', ' l ', max, ' o ', ' h '} -T.pop ()#randomly deletes the elements in the collection, this method returns the number of deletes, not the none returned in Print(t)#{n, ' W ', Max, ' l ', ' o ', ' h '} - Print(Len (t))#calculating elements in a collection to t.clear () + Print(t)#set () - Print(3inchS#determines whether an element exists in the set S, TrueView Code
Python Learning Week Two