Section 7.8. Built-in Functions


7.8. Built-in Functions

7.8.1. Standard Type Functions

len()

The len() BIF for sets returns cardinality (or the number of elements) of the set passed in as the argument.

    >>> s = set(u)     >>> s     set(['p', 'c', 'e', 'h', 's', 'o'])     >>> len(s)     6


7.8.2. Set Type Factory Functions

set() and frozenset()

The set() and frozenset() factory functions generate mutable and immutable sets, respectively. If no argument is provided, then an empty set is created. If one is provided, it must be an iterable, i.e., a sequence, an iterator, or an object that supports iteration such as a file or a dictionary.

    >>> set()     set([])     >>> set([])     set([])     >>> set(())     set([])     >>> set('shop')     set(['h', 's', 'o', 'p'])     >>>     >>> frozenset(['foo', 'bar'])     frozenset(['foo', 'bar'])     >>>     >>> f = open('numbers', 'w')     >>> for i in range(5):     ...    f.write('%d\n' % i)     ...     >>> f.close()     >>> f = open('numbers', 'r')     >>> set(f)     set(['0\n', '3\n', '1\n', '4\n', '2\n'])     >>> f.close()




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