一、set集合的特性

  1. 访问速度快
  2. 天生解决重复问题

二、set变量申明

  1. s1 = set()
  2. s2 = set([1,2,3])

备注:第二种方式在set类中直接传入一个序列。

三、set类中方法大全

1.add函数:

 def add(self, *args, **kwargs): # real signature unknown
         '''--向一个set集合中添加一个元素--'''
         """
         Add an element to a set.

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

add

例:

>>> s1 = set()
>>> s1.add(1)
>>> s1
{1}

2.clear函数:

 def clear(self, *args, **kwargs): # real signature unknown
         '''--清除set集合中的所有元素--'''
         """ Remove all elements from this set. """
         pass

clear

例:

>>> s1 = set([1,2,3])
>>> s1
{1, 2, 3}
>>> s1.clear()
>>> s1
set()

3.copy函数:

 def copy(self, *args, **kwargs): # real signature unknown
        '''--返回一个浅拷贝的set集合--'''
         """ Return a shallow copy of a set. """
         pass

copy

例:

pass

4.difference函数:

 def difference(self, *args, **kwargs): # real signature unknown
          '''--比较两个set集合得到一个差集--'''
          """
          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

difference

例:

>>> s1 = set(['one','two','three'])
>>> s2 = s1.difference(['one','two'])
>>> s2
{'three'}

 5.difference_update函数:

 def difference_update(self, *args, **kwargs): # real signature unknown
         '''-新set集合与原来set相比较,从原来set集合中删除新set集合中存在的元素-'''
         """ Remove all elements of another set from this set. """
         pass    

difference_update

例:

>>> s1 = set(['one','two','three'])
>>> s1.difference_update(['one','three'])
>>> s1
{'two'}

6.discard函数:

 def discard(self, *args, **kwargs): # real signature unknown
         '''-指定删除set集合中的某个元素,如果指定元素不在set集合中,则返回一个none-'''
         """
         Remove an element from a set if it is a member.

         If the element is not a member, do nothing.
         """
         pass    

discard

例:

①指定元素存在set集合中:

>>> s1 = set(['one','two','three'])
>>> s1.discard('one')
>>> s1
{'three', 'two'}

②指定元素不存在集合中:

>>> s1 = set(['one','two','three'])
>>> a=s1.discard('four')
>>> print(a)
None

7.intersection函数:

 def intersection(self, *args, **kwargs): # real signature unknown
         '''--返回两个set集合交集--'''
         """
         Return the intersection of two sets as a new set.

         (i.e. all elements that are in both sets.)
         """
         pass

intersection

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','three'])
>>> s3 = s1.intersection(s2)
>>> s3
{'one', 'three'}

8.intersection_update函数:

 def intersection_update(self, *args, **kwargs): # real signature unknown
         '''--将老set集合的值更新为新set集合与老set集合交集--''
         """ Update a set with the intersection of itself and another. """
         pass

intersection_update

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','three'])
>>> s1.intersection_update(s2)
>>> s1
{'one', 'three'}

9.isdisjoint函数:

 def isdisjoint(self, *args, **kwargs): # real signature unknown
         '''--如果两个set集合没有交集,返回True--'''
         """ Return True if two sets have a null intersection. """
         pass

isdisjoint

例:

①没有交集:

>>> s1 = set(['one','two','three'])
>>> s2 = set([1,2,3])
>>> s1.isdisjoint(s2)
True

②有交集:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one'])
>>> s1.isdisjoint(s2)
False

 10.issubset函数:

 def issubset(self, *args, **kwargs): # real signature unknown
         '''--判断当前这个set集合是否是另一个set集合的子集--'''
         """ Report whether another set contains this set. """
         pass

issubset

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one'])
>>> s2.issubset(s1) #是子集的情况
True
>>> s1.issubset(s2) #不是子集的情况
False

11.issuperset函数:

 def issuperset(self, *args, **kwargs): # real signature unknown
         '''--判断当前这个set集合是否是另外一个set集合父集--'''
         """ Report whether this set contains another set. """
         pass

issuperset

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one'])
>>> s1.issuperset(s2)  #s1是s2的父集情况
True
>>> s2.issuperset(s1)  #s2不是s1父集的情况
False

12.pop函数:

 def pop(self, *args, **kwargs): # real signature unknown
         '''--随机删除set集合中的元素,并且返回被删除的元素--'''
         """
         Remove and return an arbitrary set element.
         Raises KeyError if the set is empty.
         """
         pass

pop

例:

>>> s1 = set(['one','two','three'])
>>> s1.pop()
'one'
>>> s1
{'three', 'two'}

13.remove函数:

def remove(self, *args, **kwargs): # real signature unknown
        '''--指定删除set集合中的元素--'''
        """
        Remove an element from a set; it must be a member.

        If the element is not a member, raise a KeyError.
        """
        pass

remove

例:

>>> s1 = set(['one','two','three'])
>>> s1.remove("one")
>>> s1
{'three', 'two'}

14.symmetric_difference函数:

def symmetric_difference(self, *args, **kwargs): # real signature unknown
       '''--两个set集合的对称集--'''
        """
        Return the symmetric difference of two sets as a new set.

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

symmetric_difference

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','two','four'])
>>> s3 = s1.symmetric_difference(s2)
>>> print(s3)
{'three', 'four'}

15.symmertric_difference_update函数:

 def symmetric_difference_update(self, *args, **kwargs):
         '''--把两个set集合的交集之外的值,更新替换当前set集合--'''
         """ Update a set with the symmetric difference of itself and another. """
         pass

symmetric_difference_update

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','two','four'])
>>> s1.symmetric_difference_update(s2)
>>> print(s1)
{'three', 'four'}

16.union函数:

 def union(self, *args, **kwargs): # real signature unknown
         '''--sets集合的并集赋值给一个新的set集合--'''
         """
         Return the union of sets as a new set.

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

union

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','two','four'])
>>> s3=s1.union(s2)
>>> print(s3)
{'one', 'three', 'four', 'two'}

 17.update函数:

 def update(self, *args, **kwargs): # real signature unknown
         '''--更新一个set集合,更新的set集合的值是当前集合和其他集合的并集--'''
         """ Update a set with the union of itself and others. """
         pass

update

例:

>>> s1 = set(['one','two','three'])
>>> s2 = set(['one','two','four'])
>>> s1.update(s2)
>>> print(s1)
{'three', 'two', 'four', 'one'}

三、总结

1.函数中有带update的函数,或者本身就是update函数,是更新这个set集合本身,并不会将得到的结果重新赋值给一个变量。

2.函数一些函数不带update的函数,如:difference(取不同),intersection(取交集),symmetric_difference(取交集之外的值),union(取并集)等,将得到的结果返回给一个变量。

python中set集合的更多相关文章

  1. python 中的集合set

    python中,集合(set)是一个无序排列,可哈希, 支持集合关系测试,不支持索引和切片操作,没有特定语法格式, 只能通过工厂函数创建.集合里不会出现两个相同的元素, 所以集合常用来对字符串或元组或 ...

  2. Python中的集合类型分类和集合类型操作符解析

    集合类型    数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements).    Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...

  3. python学习之【第七篇】:Python中的集合及其所具有的方法

    1.前言 python中的集合set与列表类似,它们最大的区别是集合内不允许出现重复元素,如果在定义时包含重复元素,会自动去重. 集合是无序的,集合中的元素必须是不可变类型.集合可以作为字典的key. ...

  4. 14.python中的集合

    什么是集合?正如其字面的意思,一堆东西集中合并到一起.乍一听貌似和容器没什么差别,嗯,好吧,集合也算是一种容器. 在学习这个容器有什么不同之前,先看看集合是如何创建的: a = set() #可变集合 ...

  5. python中的集合

    在python中,普通集合是可变数据类型 通过以下案例说明: >>> s = {1, 2, 3, 4} >>> id(s) 2108634636808 >&g ...

  6. 8、python中的集合

    集合是python中无序.可变的数据结构.集合与字典类似,集合中的元素必须是可哈希的(等同于字典中的键),也就是说集合中的元素是唯一.不可变的数据类型.这里前面说集合可变,后面又说集合中的元素不可变是 ...

  7. python中的集合、元组和布尔

    #元组,元组跟列表一样,只不过列表可读可写,而元组一般用来只读,不修改#python中不允许修改元组的数据,也包括不能删除其中的元素. t1 = ('a','b','c','d','s','a') & ...

  8. Python 中的集合 --set

    前言 在Python中,我们用[]来表示列表list,用()来表示元组tuple,那{}呢?{}不光可用来定义字典dict,还可以用来表示集合set. 集合 set 集合(set)是一个无序的不重复元 ...

  9. python 中的集合(set) 详解

    在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种. 创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方 ...

随机推荐

  1. jquery层级原则器(匹配父元素下的子元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Android使用SAX解析XML(2)

    school类包含了一个major列表,可以增加该列表的元素,以及返回该列表,还实现了Parcelable.Creator接口. package com.hzhi.my_sax; import jav ...

  3. PHP组件化开发

    设计思想中有两种极端:大而全.小而美. 一般我们常用的库是小而美,用的框架是大而全.从Symfony实现Component式开发开始,框架的组件化逐渐成为趋势.我们可以任意的组合各种Compoent来 ...

  4. 使用JPA储存Text类型的时候 出现乱码的问题

    以前遇到这类问题第一个反应就是觉得客户端和服务端的编码不一样导致的.所以一开始也是那么认为的.以为我们项目使用的是pgsql,默认的就是utf-8,然后我们使用了字符也是utf-8,并且还有一个问题就 ...

  5. GIT 查看/修改用户名和邮箱地址

    用户名和邮箱地址的作用用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变.每次commit都会用用户名和邮箱纪录.github的contributions统计就是按邮箱来统计的.查看用户 ...

  6. Android 4.0.3 CTS 测试

    Android-CTS 4.0.3测试基本配置 1. Download CTS CTS的获取方式有两种: 1.1.由Google提供 1.1.1.打开浏览器输入连接: http://source.an ...

  7. GJM : Taurus.MVC 2.0 开源发布:WebAPI开发教程 [转载]

    Taurus.MVC 2.0 开源发布:WebAPI开发教程 转载自http://www.cnblogs.com/cyq1162/p/6069020.html 因是新手  粘贴时有一个版权问题 本文原 ...

  8. asp.net mvc4 使用 System.Web.Optimization 对javascript和style的引入、代码合并和压缩的优化(ScriptBundle,StyleBundle,Bundling and Minification )

    Bundling and Minification两个单词对今天的内容有个比较好的总结. 问题所在 一. 在asp.net包括mvc项目中,引入js和css也许有人认为是个很容易和很简单操作的事情,v ...

  9. 开发 web 桌面类程序几个必须关注的细节

    HoorayOS 写了差不多快2年了,在我的坚持下也有一部分人打算着手自己也写套类似的程序,我想我可以提供一点经验. 俗话说细节决定成败,开发2年多来,我看过大大小小类似的程序不下20个,各有优点也各 ...

  10. PhotoSwipe - 移动开发必备的 iOS 风格相册

    PhotoSwipe 是一个专门针对移动设备的图像画廊,它的灵感来自 iOS 的图片浏览器和谷歌移动端图像. PhotoSwipe 提供您的访客熟悉和直观的界面,使他们能够与您的移动网站上的图像进行交 ...