zoukankan      html  css  js  c++  java
  • Python set

     

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

    基本操作

    复制代码

     s={1,2,"a"}

     type(s)

    set

    x=set("python") x {'h', 'n', 'o', 'p', 't', 'y'} y=set(["y","a",1]) y {1, 'a', 'y'} #去重 a=[1,1,2,2,3,3] b=set(a) b {1, 2, 3} y.add(2) y.add("hello") {1, 2, 'a', 'hello', 'y'} #从 set “s”中删除元素 , 如果不存在则引发 KeyError y.remove("hello") y.remove(2) {1, 'a', 'y'} #如果在 set “s”中存在元素 x, 则删除 y.discard(1) #删除并且返回 set “y”中的一个不确定的元素, 如果为空则引发 KeyError y.pop() #删除 set “y”中的所有元素并保留set y.clear() y set() y.update([3,4,5]) {1, 3, 4, 5, 'a', 'y'} len(y) 6 "x" in y False #测试是否 s 中的每一个元素都在 t 中 x=y #测试是否 y 中的每一个元素都在 x 中,即 x包含y x=y y.issubset(x) y<=x #测试是否 x 中的每一个元素都在 y中,即y包含x y.issuperset(x) y>=x #返回一个新的 set 包含 x 和 y 中的每一个元素 x=set("python1") y.union(x) x|y #返回一个新的 set 包含 x 和 y 中的公共元素 y.intersection(x) x&y #返回一个新的 set 包含 y 中有但是 x 中没有的元素 y.difference(x) y-x #返回一个新的 set 包含 x 和 y 中不重复的元素 y.symmetric_difference(x) y^x #set y的一个浅复制 z=y.copy()

    dir(set)

    ['__and__',
    '__class__',
    '__cmp__',
    '__contains__',
    '__delattr__',
    '__doc__',
    '__eq__',
    '__format__',
    '__ge__',
    '__getattribute__',
    '__gt__',
    '__hash__',
    '__iand__',
    '__init__',
    '__ior__',
    '__isub__',
    '__iter__',
    '__ixor__',
    '__le__',
    '__len__',
    '__lt__',
    '__ne__',
    '__new__',
    '__or__',
    '__rand__',
    '__reduce__',
    '__reduce_ex__',
    '__repr__',
    '__ror__',
    '__rsub__',
    '__rxor__',
    '__setattr__',
    '__sizeof__',
    '__str__',
    '__sub__',
    '__subclasshook__',
    '__xor__',
    'add',
    'clear',
    'copy',
    'difference',
    'difference_update',
    'discard',
    'intersection',
    'intersection_update',
    'isdisjoint',
    'issubset',
    'issuperset',
    'pop',
    'remove',
    'symmetric_difference',
    'symmetric_difference_update',
    'union',
    'update']

     help(set.add)

     Help on method_descriptor:

     add(...)
     Add an element to a set.
     This has no effect if the element is already present.

  • 相关阅读:
    oracle正装表达式匹配中文
    oracle利用循环批量检索对应的数据
    oracle不完全恢复
    informatica简易教程
    oracle创建用户的小问题
    宿主机sqlplus连接虚拟机oracle
    ETL采集原表语句生成
    Mac版 MicrosoftOffice2015 办公软件 破解教程
    BetterZip,支持rar等多种压缩解压方式(Xcode自身不能解压rar)
    Xcode --自动注释插件VVDocumenter-Xcode(配置须知)
  • 原文地址:https://www.cnblogs.com/xc1234/p/9128134.html
Copyright © 2011-2022 走看看