Software: Apache/2.0.54 (Unix) mod_perl/1.99_09 Perl/v5.8.0 mod_ssl/2.0.54 OpenSSL/0.9.7l DAV/2 FrontPage/5.0.2.2635 PHP/4.4.0 mod_gzip/2.0.26.1a uname -a: Linux snow.he.net 4.4.276-v2-mono-1 #1 SMP Wed Jul 21 11:21:17 PDT 2021 i686 uid=99(nobody) gid=98(nobody) groups=98(nobody) Safe-mode: OFF (not secure) /opt/doc/python-2.3.4/html/Python-Docs-2.3.4/whatsnew/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 1 PEP 218: A Standard Set DatatypeThe new sets module contains an implementation of a set datatype. The Set class is for mutable sets, sets that can have members added and removed. The ImmutableSet class is for sets that can't be modified, and instances of ImmutableSet can therefore be used as dictionary keys. Sets are built on top of dictionaries, so the elements within a set must be hashable. Here's a simple example:
>>> import sets >>> S = sets.Set([1,2,3]) >>> S Set([1, 2, 3]) >>> 1 in S True >>> 0 in S False >>> S.add(5) >>> S.remove(3) >>> S Set([1, 2, 5]) >>>
The union and intersection of sets can be computed with the
union() and intersection() methods; an alternative
notation uses the bitwise operators
>>> S1 = sets.Set([1,2,3]) >>> S2 = sets.Set([4,5,6]) >>> S1.union(S2) Set([1, 2, 3, 4, 5, 6]) >>> S1 | S2 # Alternative notation Set([1, 2, 3, 4, 5, 6]) >>> S1.intersection(S2) Set([]) >>> S1 & S2 # Alternative notation Set([]) >>> S1.union_update(S2) >>> S1 Set([1, 2, 3, 4, 5, 6]) >>>
It's also possible to take the symmetric difference of two sets. This
is the set of all elements in the union that aren't in the
intersection. Another way of putting it is that the symmetric
difference contains all elements that are in exactly one
set. Again, there's an alternative notation (
>>> S1 = sets.Set([1,2,3,4]) >>> S2 = sets.Set([3,4,5,6]) >>> S1.symmetric_difference(S2) Set([1, 2, 5, 6]) >>> S1 ^ S2 Set([1, 2, 5, 6]) >>> There are also issubset() and issuperset() methods for checking whether one set is a subset or superset of another:
>>> S1 = sets.Set([1,2,3]) >>> S2 = sets.Set([2,3]) >>> S2.issubset(S1) True >>> S1.issubset(S2) False >>> S1.issuperset(S2) True >>>
See Also:
See About this document... for information on suggesting changes. |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #13 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0269 ]-- |