集合,set(),记住:

1个特点:去重,把列表变成集合,达到自动去重操作,无序

5个关系:测试两个列表的交差并子反向差集

方法:

  • |  add(...) 常用,已存在元素去重不生效
  • |      Add an element to a set.
  • |     This has no effect if the element is already present.
    >>> list1=[3,2,1,1,2,3,4,5]
    >>> set(list1)
    {1, 2, 3, 4, 5}
    >>> list2=[3,4,5,6,7,8]
    >>> set(list1).add(2)
    >>> set(list1).add(6)
    >>> print(set(list1).add(2))
    None
    >>> print(set(list1).add(6))
    None
    >>> set1=set(list1)
    >>> set2=set(list2)
    >>> set1,set2
    ({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
    >>> set1.add(3)
    >>> print(set1.add(3)
    ... )
    None
    >>> print(set1.add(7))
    None
    >>> set1.add('aaa')
    >>> set1
    {1, 2, 3, 4, 5, 7, 'aaa'}
    >>> id(set1)
    140138768484616
    >>> set1.add('aaaa')
    >>> id(set1)
    140138768484616
    >>> set1
    {1, 2, 3, 4, 5, 7, 'aaa', 'aaaa'}
    >>> set1.add('')
    >>> set1
    {1, 2, 3, 4, 5, 7, 'aaa', 'aaaa', ''}
    >>> set1.add(7)
    >>> set1
    {1, 2, 3, 4, 5, 7, 'aaa', 'aaaa', ''}
  • 如果是字符串,则拆分成单个字符集合
  • >>> set('abc')
    {'a', 'c', 'b'}
  • |  clear(...) 清空一个集合
  • |      Remove all elements from this set.
    >>> set1.clear()
    >>> set1
    set()
  • |  copy(...) 影子复制,指向同一个内存地址
  • |      Return a shallow copy of a set. |  
    >>> list1
    [3, 2, 1, 1, 2, 3, 4, 5]
    >>> list2
    [3, 4, 5, 6, 7, 8]
    >>> set1=set(list1)
    >>> id(set1)
    140138768485512>>> set3=set1.copy()
    >>> id(set3)
    140138695576712
  • |  difference(...) 差集,格式set1.difference(set2),求in list1 not in list2的集合
  • |      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.)   
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set2
    {3, 4, 5, 6, 7, 8}
    >>> set1.difference(set2)
    {1, 2}
  • |  difference_update(...) 删除在本集合同时也在其他集合的元素,差集
  • |      Remove all elements of another set from this set. |
    >>> set1=set(list1)
    >>> set2=set(list2)
    >>> set1,set2
    ({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
    >>> set1=set(list1)
    >>> set2=set(list2)
    >>> set2.difference_update(set1)
    >>> set2
    {6, 7, 8}
  • |  discard(...) 删除一个在本集合中的元素
  • |      Remove an element from a set if it is a member.
  • |
  • |      If the element is not a member, do nothing. |  
    >>> set3=set([1,2,3,'a','b','c'])
    >>> set3.discard(1)
    >>> set3
    {2, 3, 'c', 'b', 'a'}
    >>> set3.discard('a')
    >>> set3
    {2, 3, 'c', 'b'}
    >>> set3.discard('dd')
    >>> set3
    {2, 3, 'c', 'b'}
  • |  intersection(...)并集,同时在两个集合中的元素
  • |      Return the intersection of two sets as a new set.
  • |
  • |      (i.e. all elements that are in both sets.) |  
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set2
    {3, 4, 5, 6, 7, 8}
    >>> set1.intersection(set2)
    {3, 4, 5}
    >>> set2.intersection(set1)
    {3, 4, 5}
  • |  intersection_update(...) 交集
  • |      Update a set with the intersection of itself and another. |  
    >>> set1,set2
    ({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
    >>> set1.intersection_update(set2)
    >>> set1
    {3, 4, 5}
  • |  isdisjoint(...) 返回布尔值,判断两个集合是否没有并集
  • |      Return True if two sets have a null intersection.
  • |  
    >>> set1,set2,set3,set4
    ({3, 4, 5}, {3, 4, 5, 6, 7, 8}, {2, 3, 'c', 'b'}, {'y', 'x', 'z'})
    >>> set1.isdisjoint(set2)
    False
    >>> set1.isdisjoint(set4)
    True
  • |  issubset(...) 返回布尔值,判断前一个集合是否是后一个集合的子集
  • |      Report whether another set contains this set. |  
    >>> set1
    {3, 4, 5}
    >>> set5
    {3, 4}
    >>> set5.issubset(set1)
    True
  • |  issuperset(...) 返回布尔值,判断前一个集合是否是后一个集合的父集
  • |      Report whether this set contains another set. |  
    >>> set1
    {3, 4, 5}
    >>> set5
    {3, 4}
    >>> set1.issuperset(set5)
    True
  • |  pop(...) 随机删除一个集合元素,返回被删除元素,空集合删除则报错
  • |      Remove and return an arbitrary set element.
  • |      Raises KeyError if the set is empty. |  
    >>> set1
    {3, 4, 5}
    >>> set1.pop()
    3
    >>> set1
    {4, 5}
    >>> set1.pop()
    4
    >>> set1.pop()
    5
    >>> set1.pop()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'pop from an empty set'
  • |  remove(...) 删除指定在集合中的元素
  • |      Remove an element from a set; it must be a member. |      
    >>> set1=set(list1)
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set1.remove(1)
    >>> set1
    {2, 3, 4, 5}
    >>> set1.remove('a')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'a'
  • |  symmetric_difference(...) 对称差集,集合A与集合B不相交的部分,交集的反集
  • |     Return the symmetric difference of two sets as a new set.
  • |     (i.e. all elements that are in exactly one of the sets.)     
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set6={4,5,6,7,8}
    >>> set1.symmetric_difference(set6)
    {1, 2, 3, 6, 7, 8}
    >>> set6.symmetric_difference(set1)
    {1, 2, 3, 6, 7, 8}
    >>> set1,set7
    ({1, 2, 3, 4, 5}, {'a', 'c', 'b'})
    >>> set1.symmetric_difference(set7)
    {'c', 2, 3, 1, 4, 5, 'b', 'a'}
  • |  symmetric_difference_update(...)
  • |      Update a set with the symmetric difference of itself and another.
  • |
  • |  union(...) 并集
  • |      Return the union of sets as a new set.
  • |      (i.e. all elements that are in either set.)
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set2
    {3, 4, 5, 6, 7, 8}
    >>> set1.union(set2)
    {1, 2, 3, 4, 5, 6, 7, 8}
  • |  update(...) 用交集更新到set1的集合
  • |      Update a set with the union of itself and others. |  
    >>> set1
    {1, 2, 3, 4, 5}
    >>> set2
    {3, 4, 5, 6, 7, 8}
    >>> set1.update(set2)
    >>> set1
    {1, 2, 3, 4, 5, 6, 7, 8}
    >>>

python3.x 基础三:set集合的更多相关文章

  1. python3.x 基础三:装饰器

    装饰器:本质是函数,用于装饰其他函数,在不改变其他函数的调用和代码的前提下,增加新功能 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 3.装饰函数对于被装饰函数透明 参考如 ...

  2. python3.x 基础三:函数

    1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程变成,万事皆过程,def定义过程 3.函数式编程,将某种功能封装起来,用的时候直接调用函数名,def定义函数,也叫f ...

  3. python3.x 基础三:字符集问题

    总结了一张表,更详细信息百度百科: 序号 年份 编码 标准协会 特点 二进制长度 字符长度 表现 1 1967 ASCII 美国国家标准学会(American National Standard In ...

  4. python基础三(集合、文件)

    1.集合定义 集合天生能去重,且与字典一样,无序.集合用大括号括起来,里面的元素之间用逗号分隔,要跟字典区分开. 集合定义方法:s=set() #定义一个空集合 s={'1','a','b','c', ...

  5. python3.x 基础三:文件IO

    打开文件的两种方式 1.直接打开文件并赋值给变量,打开后得到操作句柄,但不会自动关闭 file = open('文件名‘,'打开模式',’编码‘) fd = open('../config/file1 ...

  6. Python全栈开发【基础三】

    Python全栈开发[基础三]  本节内容: 函数(全局与局部变量) 递归 内置函数 函数 一.定义和使用 函数最重要的是减少代码的重用性和增强代码可读性 def 函数名(参数): ... 函数体 . ...

  7. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...

  8. C#基础课程之五集合(HashTable,Dictionary)

    HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...

  9. Ruby语法基础(三)

    Ruby语法基础(三) ​ 在前面快速入之后,这次加深对基本概念的理解. 字符串 ​ Ruby字符串可以分为单引号字符串和双引号字符串,单引号字符串效率更高,但双引号的支持转义和运行 puts '单引 ...

随机推荐

  1. The new SFCB broker fails to start with a SSL-related error: Failure setting ECDH curve name (secp22

    # openssl ecparam -list_curves secp384r1 : NIST/SECG curve over a 384 bit prime field secp521r1 : NI ...

  2. HTTP 1.1, 返回值100.

    HTTP 1.1支持只发送header信息(不带任何body信息),如果服务器认为客户端有权限请求服务器,则返回100,否则返回401.客户端如果接受到100,才开始把请求body发送到服务器. 这样 ...

  3. Visual Studio 2015 + Windows 2012 R2, c++/cli Array::Sort() 抛出异常

    在Windows7上编译就是正常. 可见Windows2012 R2缺少了一些东西. 另外,有一个现象一样,但原因不一样的 https://stackoverflow.com/questions/46 ...

  4. [Inno Setup] 开机自启动

    [icons] Name: "{userstartup}\My Program"; Filename: "{app}\MyProg.exe"; Tasks:St ...

  5. Spring Boot JPA 中transaction的使用

    文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...

  6. Element UI表格组件技巧:如何简洁实现跨页勾选、跨页统计功能

    业务场景 在使用Element UI的Table组件时,常常面对这样的业务需求: 表格数据的每一项都要提供勾选框,当切换分页时,能够记忆所有页面勾选的数据,以实现批量提交不同页面勾选数据的功能.并且, ...

  7. Mysql使用规范及建议

    MySQL数据库使用规范一.建表规约1.[强制]表达是与否概念的字段,必须使用is_xxx的方式命名,数据类型是unsigned tinyint (1表示是,0表示否) 说明:任何字段如果为非负数,必 ...

  8. IT成长中的龟兔赛跑

    IT成长中的龟兔赛跑 相信"龟兔赛跑"的故事大家都听过吧,那就让咱给各位看官讲讲我看到的一些事情吧.      最近看到很多文章总是叹息网管如何,起得比鸡早,睡得比狗晚,吃得比猪差 ...

  9. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  10. VMware的安装与部署Linux系统

            首先我们需要准备好我们将会用到的东西:VMware12.RHEL7.0         网址我就不放了,大家自行百度哟. 一.安装VMware         我们需要安装VMware ...