Tag:car value type abc att int python Create iterate exception
Set set 1, set is an unordered set of elements, the function of the Set collection class needs to be an iterator type, such as: sequence, dictionary, and then converted to an unordered set of elements. Because collections are not duplicated, you can perform deduplication on strings, lists, and tuples. (1) Create S1=set (' This is String ') ([]) set1=set (' python ') Result: Set ([' H ', ' o ', ' n ', ' P ', ' t ', ' y ']) s2=set ([x-i]) S3=set (( 1,2,3,4,3,2)) set ([1,2,3,4]) S4=set ({1:2,2:3,6:6}) dictionary assigns a value list of key values (equivalent to the Dict.keys () return value) to the set set ([1,2,6]) Note A numeric type cannot be a parameter of a collection, such as an int type. (2) Adding an element add () adds the element as a whole update () adds the elements of the element S2.add (' abc ') set (["]" ([+/-]) sets (' ABC ') set ([' A ', ' s2.update, ' B ', ' C ']) (3) Remove the element A, remove () s2.remove (' C ') set ([' A ', ', ", ' B ']) if ' C ' does not exist, throw an exception B, discard (value) S2.discard (3) C, pop () S2.pop () Ensure that the collection has elements and that the deleted element is not sure which is. If NULL is thrown Keyerror error (4) Traversal collection for I in S2:print I (5) enumeration enumerate () for Index,elem in Enumerate (2):p rint index, ': ', Elem type conversion: STR ( S2) List (s2) tuple (S2) (6) Empties the collection S1.clear () (7) copies the set S6=s2.copy () (8) The length of the combined Len (S2) 2, the special operations of the set (1) intersection, Union, and Difference S1=set ([the ]) S2=set ([2,3,4,5]) S3=S1 & S2 intersection set ([2,3]) s4=s1 | S2 set ([1,2,3,4,5]) s5=s1-s2 difference set in S1 but not in S2 set ([1]) s6=s1.difference (S2) S6 result set ([1]) with s5 (2) >, >= < < ;= !=Print s1>=s2 (3) Issupperset () parent set S11=set ([+]) print S1.issupperset (S11) Ture S1 is the parent set of S11 print S1.issupperset (S2) Falseissubset () subset S1.issubset (S2) S1 is a subset of S2 (4) in and not in member operators if S1 in S2:3, immutable set Frozenset () Fs=frozenset ([All ]) If an attempt is made to change an element in an immutable set, a attributeerror error is reported.
Data structure of Python: Collection