python数据结构--->set(集合)
2008-01-29 11:34:53 / 个人分类:开源开发类
集合:相关数据对象集.
Set Semantics
A set is, perhaps the simplest possible
container, since it contains objects in no particular order with no
particular identification. Objects stand for themselves. With a
sequence, objects are identified by position. With a mapping, objects
are identified by some key. With a set, objects
stand for themselves.
Since each object stands for itself, elements of a
set cannot be duplicated. A
list or tuple, for
example, can have any number of duplicate objects. For example, the
tuple ( 1, 1, 2, 3 ) has four
elements, which includes two copies of the integer 1;
if we create a set from this
tuple, the set will only
have three elements.
A set has large number of operations for
unions, intersections, and differences. A common need is to examine a
set to see if a particular object is a member of
that set, or if one set is
contained within another set.
A set is mutable, which means that it
cannot be used as a key for a dict for more information.) In order to use a
set as a dict key, we can
create a frozenset, which is an immutable copy of
the original set. This allows us to accumulate a
set of values to create a
dict key.
Set Literal Values
frozenset (iterable)
→ setset( iterable) →
set>>>set( ("hello", "world", "of", "words", "of", "world") )set(['world', 'hello', 'words', 'of'])
Set Operations
>>>fib=set( (1,1,2,3,5,8,13) )>>>prime=set( (2,3,5,7,11,13) )>>>fib | primeset([1, 2, 3, 5, 7, 8, 11, 13])>>>fib & primeset([2, 3, 5, 13])>>>fib - primeset([8, 1])>>>prime - fibset([11, 7])>>>fib ^ primeset([8, 1, 11, 7])
Set Comparison Operators
All of the standard comparisons (<, <=,
>, >=, ==, !=,
in, not in) work with
sets>>>craps= set( [ (1,1), (2,1), (1,2), (6,6) ] )>>>(1,2) in crapsTrue>>>(3,4) in crapsFalse>>>three= set( [ (2,1), (1,2) ] )>>>three < crapsTrue>>>three <= crapsTrue>>>craps <= crapsTrue>>>craps < crapsFalse
Set Statements
>>> even= set( range(2,38,2) )
>>> ōdd= set( range(1,37,2) )
>>> zero= set( (0,'00') )
>>> for n in even|odd|zero:
print n
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
00
Set Built-in Functions
A number of built-in functions create or process
sets.
len(object) → integerReturn the number of items of a
set.max(set) → valueWith a
set, return its largest itemmin(set) → valueWith a
set, return its smallest item.
Set Methods
A set object has a number of member
methods. In the following definitions, s is a
set object.
The following transformation functions update a
set.
s.clearRemove all items from the
set.→ sets.copyCopy the
setto make a newset. This is a shallow copy. All objects in the newsetare references to the same objects as the originalset.→ objects.popRemove an arbitrary object from the
set, returning the object. If thesetwas already empty, this will raise aKeyErrorexception.(s.addnew)Adds element
newto theset. If the object is already in theset, nothing happens.(s.removeold)Removes element
oldfrom theset. If the objectoldis not in theset, this will raise aKeyErrorexception.(s.discardold)Removes element
oldfrom theset. If the objectoldis not in theset, nothing happens.(s.updatenew) → objectMerge values from the
newsetinto the originalset, adding elements as needed. It is equivalent to the following Python statement.s |= new.(s.intersection_updatenew) → objectUpdate
sto have the intersection ofsandnew. In effect, this discards elements froms, keeping only elements which are common tonewands. It is equivalent to the following Python statement.s &= new.(s.difference_updatenew) → objectUpdate
sto have the difference betweensandnew. In effect, this discards elements fromswhich are also innew. It is equivalent to the following Python statement.s -= new.(s.symmetric_difference_updatenew) → objectUpdate
sto have the symmetric difference betweensandnew. In effect, this both discards elements fromswhich are common withnewand also inserts elements intoswhich are unique tonew. It is equivalent to the following Python statement.s ^= new.
The following accessor methods provide information about a
set.
(s.issubsetset) → booleanIf
sis a subset ofset, returnTrue, otherwise returnFalse. Essentially, this iss <= set.(s.issupersetset) → booleanIf
sis a superset of, returnsetTrue, otherwise returnFalse. Essentially, this iss >= set.(s.unionnew) → setIf
newis a properset, returns | new. Ifnewis a sequence or other iterable, make a newsetfrom the value ofnew, then return the union,s | new. This does not updates.>>>prime.union( (1, 2, 3, 4, 5) )set([1, 2, 3, 4, 5, 7, 11, 13])(s.intersectionnew) → setIf
newis a properset, returns & new. Ifnewis a sequence or other iterable, make a newsetfrom the value ofnew, then return the intersection,s & new. This does not updates.(s.differencenew) → setIf
newis a properset, returns - new. Ifnewis a sequence or other iterable, make a newsetfrom the value ofnew, then return the difference,s - new. This does not updates.(s.symmetric_differencenew) → setIf
newis a properset, returns ^ new. Ifnewis a sequence or other iterable, make a newsetfrom the value ofnew, then return the symmetric difference,s ^ new. This does not updates.- >>>help(set)可查一下
class set(object)
| set(iterable) --> set object
|
| Build an unordered collection of unique elements.
|
| Methods defined here:
|
| __and__(...)
| x.__and__(y) <==> x&y
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __contains__(...)
| x.__contains__(y) <==> y in x.
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
.............................
论坛模式 推荐 收藏 等级(1) 编辑 管理 查看(950) 评论(0)
TAG: 开源开发类
