[Python] set learning and pythonset Learning
Similar to other languages, python set is a disordered, non-repeating element set. Its basic functions include link testing and deduplication. the Set object also supports mathematical operations such as union, intersection, difference, and sysmmetric difference. sets support x in set, len (set), and for x in set. As an unordered set, sets do not record element positions or insertion points. Therefore, sets do not support indexing, slicing, or sequence-like operations. The following is a simple example. >>> X = set ('spam') >>> y = set (['h', 'A', 'M']) >>> x, y (set (['A', 'P', 's', 'M']), set (['A', 'h', 'M']) let's have some more small applications. >>> X & y # Intersection set (['A', 'M'])> x | y # Union set (['A', 'P','s ', 'H', 'M']) >>> x-y # difference set (['P', 's']) I remember how a netizen asked me how to remove repeated elements from the massive list and use hash to solve the problem. However, I felt that the performance was not very high and it was good to solve the problem with set, example: >>> a = [11,22, 33,44, 11,22] >>> B = set (a) >>> bset ([33, 11, 44, 22]) >>> c = [I for I in B] >>> c [33, 11, 44, 22] cool, just a few lines. 1.8 a collection is used to contain a group of unordered objects. To create a set, you can use the set () function and provide a series of items as follows: s = set ([,]) # creating a numeric set t = set ("Hello") # creating a unique character set is different from the list and metadata set. The set is unordered and cannot be indexed by digits. In addition, the elements in the set cannot be repeated. For example, if you check the t set value in the preceding code, the result is >>> tset (['h', 'E', 'l', 'O']). note that only one 'L' is displayed '. The Set supports a series of standard operations, including Union, intersection, difference set, and symmetric difference set. For example: a = t | s # The Union of t and s B = t & s # The intersection of t and s c = t-s # evaluate the difference set (the item is in t, but not in s) d = t ^ s # symmetric difference set (the item is in t or s, but not both) basic operation: t. add ('x') # add an item s. update ([, 42]) # add multiple remove () in s to delete one item: t. remove ('H') len (s) set length x in s test whether x is a member of s x not in s test whether x is not a member of s. issubset (t) s <= t to test whether every element in s is in t. issuperset (t) s> = t test whether every element in t is in s. union (t) s | t returns a new set containing every element s in s and t.. Intersection (t) s & t returns a new set containing s and s. difference (t) s-t returns a new set that contains elements in s but not in t s. symmetric_difference (t) s ^ t returns a new set containing the non-repeating elements s in s and t. copy () returns a shortest copy of set "s". Note: non-operators (non-operator, is like s. the union () version will accept any iterable as a parameter. Conversely, operator-based counterparts requires that the parameters must be sets. This avoids potential errors, such as using set ('abc') & 'cbs 'to replace set ('abc'). intersection ('cbs') for better readability '). Changes made from version 2.3.1: all previous parameters must be sets. In addition, both Set and ImmutableSet support comparison between set and set. Two sets are equal only in this case: each set element is an element in the other (the two are subsets ). A set is smaller than another set. Only when the first set is the subset of the second set (a subset, but not equal ). A set is better than another set, only when the first set is the superset of the second set (it is a superset, but not equal ). Sub-set and equal comparison do not produce a complete sorting function. For example, any two sets are not equal and are not sub-sets. Therefore, the following operations return False: a <B, a = B, or a> B. Therefore, the _ cmp _ method is not provided for sets. Because sets only define part of the sorting function (subset relation), the output of list. sort () method is not defined for sets list. Operator operation result hash (s) returns the hash value of s. The following table lists the operations that are unavailable to ImmutableSet when Set is available: the operator (voperator) is equivalent to the operation result s. update (t) s | = t returns the set "s" s after adding the element in set "t. intersection_update (t) s & = t. Only set "s" s containing the elements in set "t" are retained. difference_update (t) s-= t returns the set "s" s after the elements contained in the set "t" are deleted. until ric_difference_update (t) s ^ = t returns the set "s" s that contain the set "t" or set "s" element instead of both. add (x) add element xs to set "s. remove (x) removes element x from set "s". If it does not exist, KeyErrors is triggered. Discard (x) If element x exists in set "s", delete s. pop () deletes and returns an uncertain element in set "s". If it is null, KeyErrors is triggered. clear () delete all elements in set "s". Note: Non-operator versions include update (), intersection_update (), difference_update (), and effecric_difference_update () any iterable will be accepted as the parameter. Changes made from version 2.3.1: all previous parameters must be sets. Note: This module also contains a union_update () method, which is an alias for the update () method. This method is included for backward compatibility. Programmers should use the update () method more, because this method is also supported by the built-in set () and frozenset () types.
In addition, if you want to convert the data into a dictionary, list (set (['A', 'B'])