(1) The standard type operator (all collection types)
Member relationship (in, not in)
In the case of a sequence, the in and not in operators in Python determine whether an element is a member of a collection.
Set equivalence/not equivalent
Equivalence/inequality is used to compare between the same or different sets. Two sets equal means, for each set, if and only if each member in one of the collections is also a member of another collection. It can also be said that each collection must be a subset of another set, that is, the values of S <= T and s>= T are true (true), or (s <= t and s>= T) are true (true). Collection equivalence/inequality is independent of the order of the collection's type or collection member, and is related only to the elements of the collection.
Subset/Super Set
Set uses the Python comparison operator to check whether a collection is a superset or subset of other collections. The "Less than" sign (<, <=) is used to determine the subset, and the "Greater than" sign (>, >=) is used to determine the superset. "Less than" and "greater than" mean that the two sets cannot be equal when compared. Equal numbers allow a subset and superset that are not strictly defined.
The set supports strict (<) subsets and non strict (<=) subsets, as well as strict (>) superset and non strict (>=) Hyper sets. Only if the first set is a strict subset of the second set, we call the first set "less than" the second set. Similarly, only if the first set is a strict superset of the second set, we call the first set "greater than" the second set.
Comparison of object values
Any object of the same type can be compared, in the form: a = = B, the type does not have a Boolean value before python2.3, and the return value of 1 0;2.3 is only True False
Comparison of object identities
Obj1 is Obj2--obj and obj2 are the same object return True False
Obj1 is not obj2--obj and obj2 are the same object return True False
The and, or, non-in Boolean type--python
The precedence order of the Boolean type is: not
and
Or
--Implement function as logical non-or
Standard type of built-in functions
CMP (OBJ1, OBJ2)--1>2 return i>0
1<2 return i<0
1==2 return i=0
Repr (obj)/repr (' obj ')--Returns a string representation of an object
STR (OBJ)--Returns a string representation of an object's readability
Type (obj)--Returns the types of the object
(2) Collection type operator (all collection types)
Union (|)
Union (Union) operations and sets of or (also known as inclusive disjunction) are actually equivalent, a union of two sets is a new collection in which each element is at least a member of one of the collections, that is, a member of one of the two collections. The Union symbol has an equivalence method: Union ().
Intersection (&)
You can manipulate the intersection operation against the and (or conjunction) of the set. The intersection of two sets is a new collection in which each element is a member of two sets, which is a member of two collections. The intersection symbol has an equivalent method: intersection ().
Difference complement/relative complement (–)
The difference complement or relative complement of two sets (S and T) refers to a set C, the elements in the set, belonging only to the set S, not to the set T. The difference symbol has an equivalent method: Difference ().
Symmetric difference (^)
Similar to other Boolean set operations, the symmetric difference is the XOR of the set (also known as "XOR" (exclusive disjunction)). The symmetric difference of two sets (S and T) refers to another set C, the elements of which can only be members of a set S or a set T, and cannot belong to two sets at the same time. There is an equivalent method of symmetric difference: symmetric_difference ().
Mixed collection type operations
The resulting type is the same if the left and right two operands are of the same type, both variable or mutable. But if the left and right two operands are of different types (the left-hand operand is set, the right-hand operand is frozenset, or vice versa), the resulting type is the same as the type of the left operand.
Note: The plus sign is not an operator of the collection type.
>>> T | S
Frozenset ([' C ', ' B ', ' e ', ' h ', ' k ', ' o ', ' P ', ' s '])
>>> T ^ s
Frozenset ([' C ', ' B ', ' e ', ' k ', ' P '])
>>> S | T
Set ([' C ', ' B ', ' e ', ' h ', ' k ', ' o ', ' P ', ' s '])
>>> S ^ t
Set ([' P ', ' B ', ' e ', ' k ', ' C '])
(3) Collection type operator (for variable collections only)
(Union) Update (|=)
This update method adds (possibly multiple) members from an existing collection, and this method is equivalent to update ().
>>> s = set (' Cheeseshop ')
>>> s |= set (' PyPI ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' P ', ' s ', ' Y '])
Retention/intersection update (&=)
The hold (or intersect update) operation retains a shared member of the other collection, and this method is equivalent to the intersection_update ().
Difference Update (–=)
For the difference update operation s-=t the set S and T, the difference update operation returns a collection in which the member is the set S to remove the elements remaining after the elements in the set T. This method
and Difference_update () equivalence.
Symmetric differential Update (^=)
A symmetric differential update operation (S^=T) is performed on the set S and T, and the symmetric differential update operation returns a collection that is only a member of the original set S or only another set T. This method and Symmetric_difference_update () are equivalent
The actual operation of the collection type:
(1) How to create a collection type and assign a value to a collection
The collection differs from the list ([]) and the dictionary ({}) without special syntax formatting. Lists and dictionaries can be created separately with their own Factory method list () and Dict (), which is the only way the collection is created: the Set () and Frozenset () of the collection's factory method.
>>> s = set (' Cheeseshop ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s '])
>>> t = frozenset (' bookshop ')
>>> T
Frozenset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s '])
>>> Len (s), Len (t)
(6, 6)
>>> s = = t
False
(2) How to access the values in the collection
You can traverse the view of a collection member or check whether an element is a member of a collection.
>>> ' K ' in t
True
>>> for I in S:
... print I
...
C
E
H
O
P
S
(3) How to update the collection
Adds and deletes members of a collection with methods and operators built into various collections. Attempting to modify an immutable set throws an exception only if the variable set can be modified.
[HTML] View plaincopy on code to see a snippet derived from my Code slice
>>> s.add (' z ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s ', ' Z '])
>>> s.update (' PyPI ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' P ', ' s ', ' y ', ' z '])
>>> s.remove (' z ')
>>> S-= set (' PyPI ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' s '])
(4) How to delete members and collections in a collection
Deletes the collection member remove () built-in method. Deleting the collection itself can remove the collection from its scope or call Del to clear out the current namespace just as you would any Python object. If its reference count is zero, it is also marked for garbage collection. such as Del S.
Instance:
Set () and Frozenset () using the factory method of the collection:
>>> s = set (' Cheeseshop ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s '])
>>> t = frozenset (' bookshop ')
>>> T
Frozenset ([' B ', ' H ', ' k ', ' o ', ' P ', ' s '])
>>> type (s)
<type ' Set ' >
>>> type (t)
<type ' Frozenset ' >
How to update collections add and remove members of a collection with methods and operators built in various collections:
>>> s.add (' z ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' P ', ' s ', ' Z '])
>>> s.update (' PyPI ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' P ', ' s ', ' y ', ' z '])
>>> s.remove (' z ')
>>> s
Set ([' C ', ' e ', ' I ', ' h ', ' o ', ' P ', ' s ', ' Y '])
>>> S-= set (' PyPI ')
>>> s
Set ([' C ', ' e ', ' h ', ' o ', ' s '])
Delete Collection
del s
Member relationship (in, not in)
>>> s = set (' Cheeseshop ')
>>> t = frozenset (' bookshop ')
>>> ' K ' in S
False
>>> ' K ' in t
True
>>> ' C ' not in t
True
Set equivalence/not equivalent
>>> s = = t
False
>>> s!= t
True
>>> u = frozenset (s)
>>> s = u
True
>>> set (' posh ') = = set (' Shop ')
True
A difference or relative complement of two sets (S and T) is a set C, an element of the set that belongs to only set S, not
to the set T. The difference sign has an equivalent method,
Difference ().
>>> s-t
Set ([' C ', ' e '])
Symmetric difference (^): Symmetric difference is a set of XOR the above article is the actual application of the Python dictionary to the collection type