In-depth analysis of the set type operators and Python operators in python

Source: Internet
Author: User

In-depth analysis of the set type operators and Python operators in python

(1) standard type operators (all set types)
Member relationship (in, not in)
In sequence, the in and not in operators in Python determine whether an element is a member of a set.
Set equivalence/non-equivalence
Equivalence/non-equivalence is used for comparison between the same or different sets. Two sets are equal. For each set, if and only when each member of one set is also a member of another set. It can also be said that each set must be a subset of another set, that is, the values of s <= t and s> = t are True ), or (s <= t and s> = t) is True ). The Set equivalence/non-equivalence is irrelevant to the set type or the sequence of the Set members. It is only related to the set elements.
Subset/superset
Set uses the Python comparison operator to check whether a set is a superset or a subset of another set. The "less than" symbol (<, <=) is used to determine the subset, and the "greater than" symbol (>,> =) is used to determine the superset. "Less than" and "greater than" mean that the two sets cannot be equal during comparison. The equal sign allows non-strictly defined Subsets and supersets.
Set supports strict (<) Subsets and non-strict (<=) subsets, and strict (>) supersets and non-strict (> =) supersets. Only when the first set is a strict grid set of the second set can we call the first set "less than" the second set. Similarly, only when the first set is the strict superset of the second set is called the first set "greater than" the second set.

Object Value Comparison

Any object of the same type can be compared. The format is a = B. The type does not have a Boolean value before python2.3, and the returned value is 1 0. After Version 2.3, only True False is returned.

Object Identity comparison

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 -- And, Or, non

The priority of the boolean type is not.

And

Or

-- Implement functions as logical non-sum or

Standard 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 the string representation of an object.

Str (obj) -- returns the readable string representation of the object.

Type (obj) -- type of the returned object

(2) set type operators (all set types)
Union (|)
Union operations are equivalent to the OR (also known as the parallel disjunction) of a set. The union of the two sets is a new set, each element in the set must be at least a member of one of the sets, that is, a member of one of the two sets. Union symbols have an equivalent method: union ().
Intersection (&)
The intersection operation can be compared to the AND (or union) Operation of the set. The intersection of the two sets is a new set. Each element in the set is a member of both sets, that is, a member of both sets. The intersection symbol has an equivalent method: intersection ().
Difference population/relative population (-)
The difference or relative supplement set of two sets (s and t) refers to a set C. The elements in the set belong to only the set s, not the set t. The difference symbol has an equivalent method: difference ().
Symmetric Difference (^)
Similar to other Boolean set operations, symmetric difference is the XOR (exclusive disjunction) of the Set )). Symmetric Difference between two sets (s and t) refers to another set C. The elements in this set can only belong to the set s or the set t, and cannot belong to both sets. Symmetric Difference has an equivalent method: symmetric_difference ().
Hybrid set type operation
If the two operands have the same type, both of which are mutable sets or immutable sets, the result types are the same. However, if the Left and Right operands have different types (the left operand is set, the right operand is frozenset, or the opposite), The result type is the same as that of the left operand.

Note: the plus sign is not an operator of the set 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) set type operators (only applicable to variable sets)
(Union) Update (| =)
This update method adds (possibly multiple) members from an existing set. This method is equivalent to update.

  >>> s = set('cheeseshop')   >>> s |= set('pypi')   >>> s   set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y']) 

Reserved/intersection Update (& =)
This method is equivalent to intersection_update.
Differential Update (-=)
Perform the difference update operation s-= t on the set s and t. The difference update operation returns a set. The members in the Set s are the elements remaining after the elements in the Set t are removed from the set s. This method
It is equivalent to difference_update.
Symmetric differential Update (^ =)
Perform symmetric differential Update (s ^ = t) on the set s and t. The symmetric differential update operation returns a set, the members in this set are only members of the original set s or only members of the other set t. This method is equivalent to javasric_difference_update.

 

Related Article

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.