您的位置:首页 > 编程语言 > Python开发

Python 集合类(set)学习

2014-04-10 17:34 706 查看

集合类型    

    数学上, 把set 称做由不同的元素组成的集合,集合(set)的成员通常被称做集合元素(set elements)。Python 把这个概念引入到它的集合类型对象里。集合对象是一组无序排列的可哈希的值。是的,集合成员可以做字典中的键。数学集合转为Python 的集合对象很有效,集合关系测试和union、intersection 等操作符在Python 里也同样如我们所预想地那样工作。

    和其他容器类型一样,集合支持用 in 和 not in 操作符检查成员, 由 len() 内建函数得到集合的基数(大小), 用for 循环迭代集合的成员。但是因为集合本身是无序的,你不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。

    集合(sets)有两种不同的类型,可变集合(set) 和 不可变集合(frozenset)。如你所想,对可变集合(set),你可以添加和删除元素,对 不可变集合(frozenset)则不允许这样做。请注意,可变集合(set)不是可哈希的,因此既不能用做字典的键也不能做其他集合中的元素。不可变集合(frozenset)则正好相反,即,他们有哈希值,能被用做字典的键或是作为集合中的一个成员。

集合操作符和关系符号。【原文来自python核心编程第二版】



class set(object)方法详解

 |  set() -> new empty set object 

 |  set(iterable) -> new set object

 |  # 集合创建的唯一方法-用工厂方法set()和frozenset()

 |  Build an unordered collection of unique elements.

 |  

 |  Methods defined here:
 |  add(...)

 |      Add an element to a set.

 |      # 添加成员,如果元素已存在,则不会改变

 |      This has no effect if the element is already present. 

>>> a = set("hello")
>>> a
set(['h', 'e', 'l', 'o'])
>>> a.add("h")
>>> a
set(['h', 'e', 'l', 'o'])
>>>
 |  clear(...)

 |      Remove all elements from this set.

 |      # 删除所有元素

 |  copy(...)

 |      Return a shallow copy of a set.

 |      # 浅复制 参考我的另一篇:点击打开链接 比工厂方法快

 |  difference(...)

 |      Return the difference of two or more sets as a new set.

 |      # 差补/相对补集(-)即只属于其中一个集合而且不属于另外一个集合。

 |      (i.e. all elements that are in this set but not the others.)

 |  

 |  difference_update(...)

 |      Remove all elements of another set from this set.
    # 差更新 ( –= )对集合s 和t 进行差更新操作s-=t,差更新操作会返回一个集合,该集合中的成员是集合s 去除掉集合t
中元素后剩余的元素。
>>> a = set("hello")
>>> b = set("world")
>>> a -= b
>>> a
set(['e', 'h'])


 |  discard(...)

 |      Remove an element from a set if it is a member.

 |      # s.discard(obj) 如果obj是集合s中的元素,从集合s中删除对象obj

 |      If the element is not a member, do nothing.

 |  

 |  intersection(...)

 |      Return the intersection of two or more sets as a new set.

 |      # 求交集(&)(and)

 |      (i.e. elements that are common to all of the sets.)

 |  

 |  intersection_update(...)

 |      Update a set with the intersection of itself and another.
        # 保留/交集更新( &= )保留(或交集更新)操作保留与其他集合的共有成员。
>>> a = set("hello")
>>> b = set("world")
>>> a &= b
>>> a
set(['l', 'o'])


 |  isdisjoint(...)

 |      Return True if two sets have a null intersection.

 |     # 

 |  issubset(...)

 |      Report whether another set contains this set.

 |      # s.issubset(t),如果s是t的子集,则返回True,否则返回False

 |  issuperset(...)

 |      Report whether this set contains another set.

 |      # s.issuperset(t) 如果t 是s 的超集,则返回True,否则返回False

 |  pop(...)

 |      Remove and return an arbitrary set element.

 |      Raises KeyError if the set is empty.

 |      # 删除任意一个元素,并返回这个元素,参数必须是可哈希的。

 |  remove(...)

 |      Remove an element from a set; it must be a member.

 |      # 删除一个已存在元素,参数必须是可哈希的。

 |      If the element is not a member, raise a KeyError.

 |  

 |  symmetric_difference(...)

 |      Return the symmetric difference of two sets as a new set.

 |      # 对称差分(^)即异或(XOR),也就是求异去同,保留两个集合中不同的元素,去掉都存在的元素。

 |      (i.e. all elements that are in exactly one of the sets.)

 |  

 |  symmetric_difference_update(...)

 |      Update a set with the symmetric difference of itself and another.

        # 对称差分更新( ^= )对集合s 和t 进行对称差分更新操作(s^=t),对称差分更新操作会返回一个集合,该集合中的成员仅是原集合s 或仅是另一集合t 中的成员。

>>> a = set("hello")
>>> b = set("world")
>>> a ^= b
>>> a
set(['e', 'd', 'h', 'r', 'w'])


 |  union(...)

 |      Return the union of sets as a new set.

 |      # 求并集(|)(OR)

 |      (i.e. all elements that are in either set.)

>>> a = set("hello")
>>> b = set("world")
>>> c = a|b
>>> a
set(['h', 'e', 'l', 'o'])
>>> b
set(['d', 'r', 'o', 'w', 'l'])
>>> c
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
>>> c = a.union(b)
>>> c
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
>>> c = b.union(a)
>>> c
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
>>> c = a&b
>>> c
set(['l', 'o'])
>>> c = a.intersection(b)
>>> c
set(['l', 'o'])
>>> c = b.intersection(a)
>>> c
set(['l', 'o'])
>>> c = a - b
>>> c
set(['h', 'e'])
>>> c = b - a
>>> c
set(['r', 'd', 'w'])
>>> c = a^b
>>> c
set(['e', 'd', 'h', 'r', 'w'])
>>> 
 |  update(...)

 |      Update a set with the union of itself and others.

        # (Union) Update ( |= )这个更新方法从已存在的集合中添加(可能多个)成员

>>> a = set("hello")
>>> b = set("world")
>>> a
set(['h', 'e', 'l', 'o'])
>>> b
set(['d', 'r', 'o', 'w', 'l'])
>>> a |= b
>>> a
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])


 |  ----------------------------------------------------------------------

 |  Data and other attributes defined here:

 |  

 |  __hash__ = None

 |  

 |  __new__ = <built-in method __new__ of type object>

 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

混合集合类型

可变的set()和不可变的frozenset()操作,如果左右两个操作数的类型相同,既都是可变集合或不可变集合, 则所产生的结果类型是相同的,但如果左右两个操作数的类型不相同(左操作数是set,右操作数是frozenset,或相反情况),则所产生的结果类型与左操作数的类型相同。

相关模块





说明:本文只是初略整理,没有实践过,可作参考,不保证正确的程度!
后期保持更新,时间不定。
转载请带上本文地址:http://blog.csdn.net/zhanh1218/article/details/23331329

本文由@The_Third_Wave原创。不定期更新,有错误请指正。

Sina微博关注:@The_Third_Wave 

如果这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏!如果您一定要转载,请带上后缀和本文地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python set 集合