Python 集合操作

集合 set

集合用于包含一组无序的对象
与列表和元组不同,集合是无序的,也无法通过数字进行索引。
集合中的元素不能重复
set和dict一样,只是没有value,相当于dict的key集合
由于dict的key是不重复的,且key是不可变对象因此set也有如下特性:

▷不重复,(互异性),也就是说集合是天生去重的
▷元素为不可变对象,(确定性,元素必须可hash)
▷集合的元素没有先后之分,(无序性)

python3.x集合的内置函数有17个

class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
    
    Build an unordered collection of unique elements.
    """
是把要传入的元素做为一个整个添加到集合中
def add(self, *args, **kwargs): # real signature unknown
    """
    Add an element to a set.
    
    This has no effect if the element is already present.
    """
    pass
清空集合里面的所有元素
def clear(self, *args, **kwargs): # real signature unknown
    """ Remove all elements from this set. """
    pass
复制集合里面的所有元素 ,返回一个浅复制
def copy(self, *args, **kwargs): # real signature unknown
    """ Return a shallow copy of a set. """
    pass
求两个集合里面的不同的元素 ,又称差
def difference(self, *args, **kwargs): # real signature unknown
    """
    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.)
    """
    pass
返回删除了 set “集合2”中含有的元素后的 set “集合1”
def difference_update(self, *args, **kwargs): # real signature unknown
    """ Remove all elements of another set from this set. """
    pass
如果在 set “集合”中存在元素 x, 则删除
def discard(self, *args, **kwargs): # real signature unknown
    """
    Remove an element from a set if it is a member.
    
    If the element is not a member, do nothing.
    """
    pass
求两个集合里面相同的元素,又称并. 返回只保留含有 set “集合2”中元素的 set “集合1”
def intersection(self, *args, **kwargs): # real signature unknown
    """
    Return the intersection of two sets as a new set.
    
    (i.e. all elements that are in both sets.)
    """
    pass
返回只保留含有 set “集合2”中元素的 set “集合1” ,并更新自己
def intersection_update(self, *args, **kwargs): # real signature unknown
    """ Update a set with the intersection of itself and another. """
    pass
判断两个集合是不是不相交,并返回
def isdisjoint(self, *args, **kwargs): # real signature unknown
    """ Return True if two sets have a null intersection. """
    pass
判断集合是不是包含其他集合,等同于a>=b
def issubset(self, *args, **kwargs): # real signature unknown
    """ Report whether another set contains this set. """
    pass
判断集合是不是被其他集合包含,等同于a<=b
def issuperset(self, *args, **kwargs): # real signature unknown
    """ Report whether this set contains another set. """
    pass
删除并且返回 set “集合”中的一个不确定的元素, 如果为空则引发 KeyError
def pop(self, *args, **kwargs): # real signature unknown
    """
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.
    """
    pass
从 set “集合”中删除元素 , 如果不存在则引发 KeyError
def remove(self, *args, **kwargs): # real signature unknown
    """
    Remove an element from a set; it must be a member.
    
    If the element is not a member, raise a KeyError.
    """
    pass
返回一个新的 set 包含外面和里面中不重复的元素,也就是两个集合不重复的元素
def symmetric_difference(self, *args, **kwargs): # real signature unknown
    """
    Return the symmetric difference of two sets as a new set.
    
    (i.e. all elements that are in exactly one of the sets.)
    """
    pass
返回含有 set “里面”或者 set “外面”中有而不是两者都有的元素的 set “外面”
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
    """ Update a set with the symmetric difference of itself and another. """
    pass
把两个集合连接起来,又称并
def union(self, *args, **kwargs): # real signature unknown
    """
    Return the union of sets as a new set.
    
    (i.e. all elements that are in either set.)
    """
    pass
把两个集合连接起来,又称并
def update(self, *args, **kwargs): # real signature unknown
    """ Update a set with the union of itself and others. """
    pass

1.定义集合

a1=[1,2,3,4]
print(set(a1))
>>> {1, 2, 3, 4}
#或者直接定义一个集合
a2_set={1,2,3,4,5}

2.增(更新)

set.add()
set.update([])
# set.add() 只能增加一个, 不能同时增加多个值
a1={1,2,3,4}
a1.add(8)
a1
>>> {1, 2, 3, 4, 8}
--------------------------------------
a1.add(11,12)    # 同时增加多个值报错
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: add() takes exactly one argument (2 given)
--------------------------------------
""" Update a set with the union of itself and others. """
# a2 集合与 a1 集合 合并更新成为新的 a2 集合
a1={1,2,3,4}
a2={11,12,13}
a2.update(a1)
a2
>>> {1, 2, 3, 4, 8, 11, 12, 13}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • list的集合运算,可以先把list转化为集合,然后在用集合的运算法则 1.如下所示:俩个列表a,b 2. 将列表...
    Seizens_Swift阅读 1,751评论 0 2
  • 一、python 变量和数据类型 1.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序...
    绩重KF阅读 5,902评论 0 1
  • 最近在慕课网学习廖雪峰老师的Python进阶课程,做笔记总结一下重点。 基本变量及其类型 变量 在Python中,...
    victorsungo阅读 5,805评论 0 5
  • 在我周围甚至更大的范围内,大家都喜欢吃东北的大米,也曾听过南方的人说南方米不好吃,知道这是为什么吗?这是因为北方水...
    王了一一阅读 1,639评论 2 5
  • 宝宝今天来到这个世界已经第22天了,我是个没有持续性的人,曾坚定的要记录怀孕日记,然而,到宝宝出生了,才想起提笔。...
    小葵向阳阅读 1,688评论 0 0

友情链接更多精彩内容