集合,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. 基于java的OpenCV安装和配置

    目录 OpenCV简介 OpenCV下载安装 eclipse里引用jar包和配置 OpenCV简介 OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Window ...

  2. 聚集表(clustered table)data page中的数据行可以无序

    误区 一直以为只要一个表含有聚集索引,那么在data page中的数据行是排序的.比如原来data page中有1.2.4.5.6这样四条记录,那么我要插入3这条记录,应该是先将456三条记录往后移, ...

  3. 分享一批国内常用的tracker地址

    本期先分享一批国内能用地址,下一期我会出一期取代迅雷的下载的工具教程. udp://p4p.arenabg.com:1337/announce udp://tracker.tiny-vps.com:6 ...

  4. 洛谷 2016 战略游戏(树形DP)

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题. 他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能 ...

  5. 2019 ICPC 银川网络赛 F-Moving On (卡Cache)

    Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each city has a ...

  6. 一个简单的wed服务器SHTTPD(5)————服务器SHTTPD请求方法解析

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  7. Vxlan L2

    VXLAN(Virtual eXtensible LAN可扩展虚拟局域网)诞生了,基于IP网络之上,采用的是MAC in UDP技术 跨三层实现二层通信 总结为何需要Vxlan: 虚拟机规模受到网络规 ...

  8. Java——字节和字符的区别

    字节 1.bit=1  二进制数据0或1 2.byte=8bit  1个字节等于8位 存储空间的基本计量单位 3.一个英文字母=1byte=8bit 1个英文字母是1个字节,也就是8位 4.一个汉字= ...

  9. HashMap 底层探索

    其实HashMap就是一个Node数组,只是这个数组很奇怪它的每一个Node节点都有自己的下一个Node;这个是hashMap的Node的源码: static class Node<K,V> ...

  10. Scrapy模块使用出错,出现builtins.ImportError: DLL load failed: 找不到指定的程序

    问题描述:初次学习scrapy,使用scrapy官方文档创建爬虫项目出错, 出现builtins.ImportError: DLL load failed: 找不到指定的程序, ImportError ...