Deep parsing of collection type operators in Python

Source: Internet
Author: User
(1) Standard type operator (all collection types)
Member relationship (in, not in)
In terms of sequences, the in and not in operators in Python determine whether an element is a member of a collection.
set equivalence/not equivalent
Equivalence/non-equivalence is used to compare between the same or different sets. Two set equality means, for each collection, when 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 both true (true), or (s <= t and s>= T) the value is True (true). Set equivalence/inequality is independent of the order of the collection's type or collection members and is only relevant to the elements of the collection.
subsets/Hyper-sets
Set uses the Python comparison operator to check whether a collection is a superset or a subset of other collections. The "less than" symbol (<, <=) is used to determine the subset, and the "Greater than" sign (>, >=) is used to determine the superset. "Less than" and "greater than" mean that two collections cannot be equal at the time of comparison. The equals sign allows non-strictly defined subsets and superset.
Set supports strict (<) subsets and non-strict (<=) subsets, and also supports strict (>) superset and non-strict (>=) superset. It is only when the first collection is a strict subset of the second set that we call the first set "less than" the second collection. Similarly, only if the first collection is a strict superset of the second set, we call the first set "greater than" the second collection.

Comparison of object values

Any object of the same type can be compared in the form of a = = B, the type does not have a Boolean value before python2.3, and the return value is 1 0;2.3 after the version returns 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

Boolean type--python and, or, non-

The precedence of a Boolean type is: not

and

Or

--Implementing functions as logical non-and OR

Built-in functions of standard types

CMP (OBJ1, OBJ2)--1>2 return i>0  1<2 return i<0 1==2 return i=0

Repr (obj)/repr (' obj ')--Returns the string representation of an object

STR (OBJ)--Returns a string representation of the object's readability

Type (obj)--return types of objects

(2) Collection type operator (all collection types)
Union (|)
Union (Union) operations and sets of or (also called inclusive disjunction) are actually equivalent, and the union of two sets is a new collection, each of which is a member of at least one of the collections, that is, one of the two collections. The Union symbol has an equivalent method: Union ().
Intersection (&)
The intersection operation can be compared to an and (or a close) operation of the collection. The intersection of two sets is a new collection in which each element is a member of the two collection, which is a member of two collections. The intersection symbol has an equivalent method: intersection ().
Differential complement/relative complement set (–)
The difference or relative complement of two sets (S and T) refers to a set of C, which is an element in a collection that belongs only to the set S and not to the collection T. The difference symbol has an equivalent method: Difference ().
Symmetric differential (^)
Similar to other Boolean set operations, symmetric differencing is a set of XOR (also known as "XOR" (exclusive disjunction)). The symmetric difference of two sets (S and T) refers to another set of C, in which the elements of the collection can only be members belonging to the set S or set T, and cannot belong to two collections at a time. There is an equivalent method for symmetric difference: symmetric_difference ().
Mixed collection type operations
If the type of the left and right two operands is the same, both a mutable or an immutable collection, the resulting type is the same. However, if the type of the left and right two operands is different (the number is set, the rvalue is Frozenset, or vice versa), the resulting type is the same as 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 ']) c4/>>>> S | T   set ([' C ', ' B ', ' e ', ' h ', ' k ', ' o ', ' P ', ' s '])   >>> s ^ t   

(3) Set type operator (for mutable 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   

Retention/intersection update (&=)
The preserve (or intersect update) operation retains a common member with other collections, and this method is equivalent to Intersection_update ().
Poor update (–=)
For the difference update operation of the set S and T s-=t, the update operation returns a collection in which the members of the collection are the elements remaining after the elements in the set T are removed. This method
and Difference_update () are equivalent.
Symmetric differential Update (^=)
Symmetric differential Update operation (S^=T) for sets S and T, the symmetric differential update operation returns a collection of members that are only the original set S or only members of another set T. This method is equivalent to Symmetric_difference_update ().

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.