Collection Built-in Functions and Built-in Methods
(1) Standard Type Functions
len(): pass the set as an argument to the built-in function len(), which returns the base of the set (or the number of elements).
(2) Collection type factory function
The set() and frozenset() factory functions are used to generate mutable and immutable sets, respectively. If no argument is provided, the empty collection is generated by default. If an argument is provided, it must be iterable, i.e., a sequence or iterator or an object that supports iteration, such as a file or a dictionary.
(3) Methods (all set methods)
(t) Returns True if s is a subset of t, False otherwise.
(t) Returns True if t is a superset of s, False otherwise.
(t) returns a new set which is the union of s and t
(t) Returns a new set which is the intersection of s and t.
(t) returns a new set that is a member of s, but not of t
s.symmetric_difference(t) returns a new set that is a member of either s or t, but is not shared by s and t
() Returns a new set that is a shallow copy of the set s.
The built-in method copy() has no equivalent operator. Like the dictionary method of the same name, the copy() method is faster than copying a copy of an object with a factory method like set(), frozenset(), or dict().
(4) Methods (only for mutable collections)
Methods for mutable collection types:
Demo Example:
I. Collection type methods
>>> s = set('cheeseshop') >>> t = set('bookshop') >>> s set(['c', 'e', 'h', 'o', 'p', 's']) >>> t set(['b', 'h', 'k', 'o', 'p', 's']) >>> (t) False >>> (t) False >>> (t) set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's']) >>> (t) set(['h', 's', 'o', 'p']) >>> (t) set(['c', 'e']) >>> s.symmetric_difference(t) set(['b', 'e', 'k', 'c']) >>> () set(['p', 'c', 'e', 'h', 's', 'o'])
II. Methods for variable collection types
1, (t) - modifies s with elements from t, i.e., s now contains members of s or t.
>>> (t) >>> s set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])
2. s.intersection_update(t) - the members of s are elements that belong together in s and t.
>>> s = set('cheeseshop') >>> t = set('bookshop') >>> s.intersection_update(t) >>> s set(['h', 's', 'o', 'p'])
3. s.difference_update(t) - members of s are elements that belong to s but are not contained in t.
>>> s = set('cheeseshop') >>> t = set('bookshop') >>> s.difference_update(t) >>> s set(['c', 'e'])
4. s.symmetric_difference_update(t) - members of s are updated to those elements that are contained in s or t, but are not common to s and t.
>>> s = set('cheeseshop') >>> t = set('bookshop') >>> s.symmetric_difference_update(t) >>> s set(['c', 'b', 'e', 'k'])
5.(obj) - Add object obj to collection s.
>>> ('o') >>> s set(['c', 'b', 'e', 'k', 'o'])
6, (obj) - removes the object obj from the collection s. If obj is not an element of the collection s (obj not in s), a KeyError is raised.
<p>>>> ('b') >>> s set(['c', 'e', 'k', 'o']) >>> ('a')</p><p>Traceback (most recent call last): File "<pyshell#53>", line 1, in <module> ('a') KeyError: 'a' </p>
7.(obj) - Remove object obj from collection s if obj is an element of collection s.
>>> ('a') >>> s set(['c', 'e', 'k', 'o']) >>> ('e') >>> s set(['c', 'k', 'o'])
8, ()-removes any object in the collection is and returns it.
>>> () 'c' >>> s set(['k', 'o'])
9, ()-deletes all elements in the set s.
>>> () >>> s set([])