博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Set
阅读量:4029 次
发布时间:2019-05-24

本文共 5383 字,大约阅读时间需要 17 分钟。

 

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) → set

set( 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 | prime set([1, 2, 3, 5, 7, 8, 11, 13]) >>> fib & prime set([2, 3, 5, 13]) >>> fib - prime set([8, 1]) >>> prime - fib set([11, 7]) >>> fib ^ prime set([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 craps True >>> (3,4) in craps False >>> three= set( [ (2,1), (1,2) ] ) >>> three < craps True >>> three <= craps True >>> craps <= craps True >>> craps < craps False

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 ) → integer

Return the number of items of a set.

max(set ) → value

With a set, return its largest item

min(set ) → value

With 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.clear

Remove all items from the set.

s.copy → set

Copy the set to make a new set. This is a shallow copy. All objects in the new set are references to the same objects as the original set.

s.pop → object

Remove an arbitrary object from the set, returning the object. If the set was already empty, this will raise a KeyError exception.

s.add(new)

Adds element new to the set. If the object is already in the set, nothing happens.

s.remove(old)

Removes element old from the set. If the object old is not in the set, this will raise a KeyError exception.

s.discard(old)

Removes element old from the set. If the object old is not in the set, nothing happens.

s.update(new) → object

Merge values from the new set into the original set, adding elements as needed. It is equivalent to the following Python statement. s |= new.

s.intersection_update(new) → object

Update s to have the intersection of s and new. In effect, this discards elements from s, keeping only elements which are common to new and s. It is equivalent to the following Python statement. s &= new.

s.difference_update(new) → object

Update s to have the difference between s and new. In effect, this discards elements from s which are also in new. It is equivalent to the following Python statement. s -= new.

s.symmetric_difference_update(new) → object

Update s to have the symmetric difference between s and new. In effect, this both discards elements from s which are common with new and also inserts elements into s which are unique to new. It is equivalent to the following Python statement. s ^= new.

The following accessor methods provide information about a set.

s.issubset(set) → boolean

If s is a subset of set, return True, otherwise return False. Essentially, this is s <= set.

s.issuperset(set) → boolean

If s is a superset of set, return True, otherwise return False. Essentially, this is s >= set.

s.union(new) → set

If new is a proper set, return s | new. If new is a sequence or other iterable, make a new set from the value of new, then return the union, s | new. This does not update s.

>>> prime.union( (1, 2, 3, 4, 5) ) set([1, 2, 3, 4, 5, 7, 11, 13])
s.intersection(new) → set

If new is a proper set, return s & new. If new is a sequence or other iterable, make a new set from the value of new, then return the intersection, s & new. This does not update s.

s.difference(new) → set

If new is a proper set, return s - new. If new is a sequence or other iterable, make a new set from the value of new, then return the difference, s - new. This does not update s.

s.symmetric_difference(new) → set

If new is a proper set, return s ^ new. If new is a sequence or other iterable, make a new set from the value of new, then return the symmetric difference, s ^ new. This does not update s.

>>>help(set)可查一下

Help on class set in module __builtin__:

转载地址:http://djqbi.baihongyu.com/

你可能感兴趣的文章
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming course_2_assessment_1
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming Tuple Assignment with Unpacking
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
关于共享单车定位不准问题
查看>>
终于搞定CString和string之间转换的问题了
查看>>