zoukankan      html  css  js  c++  java
  • python数据类型-集合

    集合介绍:

    集合用法:

     

    s = {}

    type(s)
    <class 'dict'>
    s={1}
    type(s)
    <class 'set'>
    l=[1,22,36,54,1,3,2,22]
    set(l)
    {1, 2, 3, 36, 54, 22}
    s={1,2,3,4,5,2,5}
    s
    {1, 2, 3, 4, 5}
    s.add(5)
    s
    {1, 2, 3, 4, 5}
    s.add(6)
    s
    {1, 2, 3, 4, 5, 6}
    s.pop()
    1
    s.pop()
    2
    s
    {3, 4, 5, 6}
    s.add(1)
    s.add(2)
    s
    {1, 2, 3, 4, 5, 6}
    s.remove(7)
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    KeyError: 7
    s.remove(5)
    s
    {1, 2, 3, 4, 6}
    s.discard(7)
    s
    {1, 2, 3, 4, 6}
    s.discard(1)
    s
    {2, 3, 4, 6}
    s.update(1,2,5,7,8)
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: 'int' object is not iterable
    s.update([1,2,5,7,8])
    s
    {1, 2, 3, 4, 5, 6, 7, 8}
    s.add(1,2,3,4)
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: add() takes exactly one argument (4 given)
    s
    {1, 2, 3, 4, 5, 6, 7, 8}
    s.clear()
    s
    set()
    help(s.update())
    Help on NoneType object:
    class NoneType(object)
     |  Methods defined here:
     |  
     |  __bool__(self, /)
     |      self != 0
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  __repr__(self, /)

     |      Return repr(self).

    集合的交集差集:(intersection、|这俩是交集;)

     

    集合的并集:

      

     对称差集:两集合相交集合之外的集合

     

    包含关系:

     

      

     

     

    总结:

    s = set([3,5,9,10])      #创建一个数值集合 

    t = set("Hello")         #创建一个唯一字符的集合 

    a = t | s              # t 和 s的并集   

    b = t & s            # t 和 s的交集 

    c = t – s             # 求差集(项在t中,但不在s中) 

    d = t ^ s            # 对称差集(项在t或s中,但不会同时出现在二者中) 

    基本操作: 

    t.add('x')            # 添加一项    

    s.update([10,37,42])  # 在s中添加多项    

    使用remove()可以删除一项:     t.remove('H')    

    len(s)              set 的长度    

    x in s              测试 x 是否是 s 的成员    

    x not in s        测试 x 是否不是 s 的成员   

    s.issubset(t) 

    s <= t 

    ->测试是否 s 中的每一个元素都在 t 中   

    s.issuperset(t) 

    s >= t 

    ->测试是否 t 中的每一个元素都在 s 中 

    s.union(t) 

    s | t 

    ->返回一个新的 set 包含 s 和 t 中的每一个元素 

    s.intersection(t) 

    s & t 

    ->返回一个新的 set 包含 s 和 t 中的公共元素 

    s.difference(t) 

    s - t 

    ->返回一个新的 set 包含 s 中有但是 t 中没有的元素   

    s.symmetric_difference(t) 

    s ^ t 

    ->返回一个新的 set 包含 s 和 t 中不重复的元素   

    s.copy() 

    ->返回 set “s”的一个浅复制 

    附加进制相关知识:

    8进制和16进制:

    2进制 : bin(num)   ;  8进制  : oct(num)  ;  16进制 : hex(num)  ;  ASCII码 :   chr()

    二进制与十六进制转换:

    https://jingyan.baidu.com/article/47a29f24292608c0142399cb.html

  • 相关阅读:
    空间谱专题16:间距选取分析
    复数矩阵分解的拆解思路(矩阵求逆/特征值分解)
    〖Linux〗Linux高级编程
    〖Linux〗安装和使用virtualenv,方便多个Python版本中切换
    〖Linux〗bash和expect执行ssh命令行sshcmd.exp
    【Linux】解决Android Stadio报错:error in opening zip file
    〖Linux〗穿越城墙之后,直接连接国内网站的路由配置
    〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)
    〖Network〗一行命令创建 http-server
    〖Linux〗多个JDK版本之间快速切换
  • 原文地址:https://www.cnblogs.com/AlbertY/p/8786329.html
Copyright © 2011-2022 走看看