Section 7.7. Set Type Operators


7.7. Set Type Operators

7.7.1. Standard Type Operators (all set types)

Membership (in, not in)

As for sequences, Python's in and not in operators are used to determine whether an element is (or is not) a member of a set.

    >>> s = set('cheeseshop')     >>> t = frozenset('bookshop')     >>> 'k' in s     False     >>> 'k' in t     True     >>> 'c'  not in t     True


Set Equality/Inequality

Equality (or inequality) may be checked between the same or different set types. Two sets are equal if and only if every member of each set is a member of the other. You can also say that each set must be a(n improper) subset of the other, e.g., both expressions s <= t and s >= t are true, or (s <= t and s >= t) is TRue. Equality (or inequality) is independent of set type or ordering of members when the sets were createdit is all based on the set membership.

    >>> s == t     False     >>> s != t     True     >>> u = frozenset(s)     >>> s == u     True     >>> set('posh') == set('shop')     True


Subset Of/Superset Of

Sets use the Python comparison operators to check whether sets are subsets or supersets of other sets. The "less than" symbols (<, <= ) are used for subsets while the "greater than" symbols (>, >= ) are used for supersets.

Less-than and greater-than imply strictness, meaning that the two sets being compared cannot be equal to each other. The equal sign allows for less strict improper subsets and supersets.

Sets support both proper (< ) and improper (<= ) subsets as well as proper (> ) and improper (>= ) supersets. A set is "less than" another set if and only if the first set is a proper subset of the second set (is a subset but not equal), and a set is "greater than" another set if and only if the first set is a proper superset of the second set (is a superset but not equal).

    >>> set('shop') < set('cheeseshop')     True     >>> set('bookshop') >= set('shop')     True


7.7.2. Set Type Operators (All Set Types)

Union ( | )

The union operation is practically equivalent to the OR (or inclusive disjunction) of sets. The union of two sets is another set where each element is a member of at least one of the sets, i.e., a member of one set or the other. The union symbol has a method equivalent, union().

    >>> s | t     set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])


Intersection ( & )

You can think of the intersection operation as the AND (or conjunction) of sets. The intersection of two sets is another set where each element must be a member of at both sets, i.e., a member of one set and the other. The intersection symbol has a method equivalent, intersection().

    >>> s & t     set(['h', 's', 'o', 'p']


Difference/Relative Complement ( - )

The difference, or relative complement, between two sets is another set where each element is in one set but not the other. The difference symbol has a method equivalent, difference().

    >>> s - t     set(['c', 'e'])


Symmetric Difference ( ^ )

Similar to the other Boolean set operations, symmetric difference is the XOR (or exclusive disjunction) of sets. The symmetric difference between two sets is another set where each element is a member of one set but not the other. The symmetric difference symbol has a method equivalent, symmetric_difference().

    >>> s ^ t     set(['k', 'b', 'e', 'c'])


Mixed Set Type Operations

In the above examples, s is a set while t is a frozenset. Note that each of the resulting sets from using the set operators above result in sets. However note that the resulting type is different when the operands are reversed:

    >>> t | s     frozenset(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])     >>> t ^ s     frozenset(['c', 'b', 'e', 'k'])     >>> t - s     frozenset(['k', 'b'])


If both types are sets or frozensets, then the type of the result is the same type as each of the operands, but if operations are performed on mixed types (set and frozenset, and vice versa), the type of the resulting set is the same type as the left operand, which we can verify in the above.

And no, the plus sign is not an operator for the set types:

    >>> v = s + t     Traceback (most recent call last):       File "<stdin>", line 1, in ?     TypeError: unsupported operand type(s) for +: 'set' and     'set'     >>> v = s | t     >>> v     set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])     >>> len(v)     8     >>> s < v     True


7.7.3. Set Type Operators (Mutable Sets Only)

(Union) Update ( | = )

The update operation adds (possibly multiple) members from another set to the existing set. The method equivalent is update().

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


Retention/Intersection Update ( & = )

The retention (or intersection update) operation keeps only the existing set members that are also elements of the other set. The method equivalent is intersection_update().

    >>> s = set(u)     >>> s &= set('shop')     >>> s     set(['h', 's', 'o', 'p'])


Difference Update ( - = )

The difference update operation returns a set whose elements are members of the original set after removing elements that are (also) members of the other set. The method equivalent is difference_update().

    >>> s = set(u)     >>> s -= set('shop')     >>> s     set(['c', 'e'])


Symmetric Difference Update ( ^ = )

The symmetric difference update operation returns a set whose members are either elements of the original or other set but not both. The method equivalent is symmetric_difference_update().

    >>> s = set(u)     >>> t = frozenset('bookshop')     >>> s ^= t     >>> s     set(['c', 'b', 'e', 'k'])




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net