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.

    
    
  • 相关阅读:
    python【第五篇】常用模块学习
    (三)训练HMM模块
    (二)杂项准备
    (四)看看成果
    (一)准备训练语音文件
    HTK语音识别示例(Ubuntu)
    RoboCup仿真3D TC笔记(2014年合肥中国公开赛 仿真3D比赛环境搭建)
    WebFont与页面font-icon图标研究
    Font Awesome使用方法
    css sprites拼合
  • 原文地址:https://www.cnblogs.com/dadadechengzi/p/6346725.html
Copyright © 2011-2022 走看看