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}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,175评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,674评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,151评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,597评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,505评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,969评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,455评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,118评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,227评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,213评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,214评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,928评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,512评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,616评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,848评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,228评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,772评论 2 339

推荐阅读更多精彩内容

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