• Python s12 Day3 笔记及作业


    1. Set集合

    old_dict = {
        "#1":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':16},
        "#2":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':16},
        "#3":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':16}
    }
    new_dict = {
        "#1":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':32},
        "#3":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':16},
        "#4":{ 'hostname':'c1', 'cpu_count':2,  'mem_capicity':16}
    }
    
    old = set(old_dict.keys())
    new = set(new_dict.keys())
    update_set = old.intersection(new)
    print("Update Set:",update_set)
    #delete_set = old.difference(update_set)    #difference是循环old,找出old中不在update_set中的元素
    delete_set = old.symmetric_difference(update_set)  #symmetric_difference是循环old和update_set, 找出两者中不在对方中的元素
    print("delete set:",delete_set)
    #add_set = new.difference(update_set)
    add_set = new.symmetric_difference(update_set)
    print("add set:",add_set)

    2. Counter计数器

    Counter是对字典类型的补充,用于追踪值的出现次数,具备字典的所有功能和自己的功能。

    import collections
    obj
    = collections.Counter('aabbsdfsdgabadsf') print(obj) ret = obj.most_common(4) print(ret)
    -----------------------------------
    Counter({'a': 4, 'b': 3, 's': 3, 'd': 3, 'f': 2, 'g': 1})
    [('a', 4), ('b', 3), ('s', 3), ('d', 3)]

    3. OrderedDict有序字典

    import collections
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic)
    ----------------------------------
    OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])

    4. defaultdict默认字典

    为字典设置默认类型

    dic = collections.defaultdict(list)
    dic['k1'].append('wayne')
    print(dic)
    -----------------------------------
    defaultdict(<class 'list'>, {'k1': ['wayne']})

    5. 可命名元组

    import collections
    #创建类
    MytupleClass = collections.namedtuple('Mytuple',['x','y','z'])
    obj = MytupleClass(11,22,33)
    print(obj.x, obj.y, obj.z)

    6. deque双向队列

    import collections
    d = collections.deque(['2', '3'])
    d.appendleft('1')
    print(d)
    d.extend('4')
    print(d)

    单向队列 Queue.Queue
    单向队列双向队列都是线程安全的

    7. 动态参数

    def show(*args, **kwargs):
        print(args, type(args))
        print(kwargs, type(args))
    
    l = [11,22,33,44]
    d = {'a':1, 'b':2}
    
    show(l, d)
    show(*l, **d)
    ------------------------------------
    ([11, 22, 33, 44], {'a': 1, 'b': 2}) <class 'tuple'>
    {} <class 'tuple'>
    (11, 22, 33, 44) <class 'tuple'>
    {'a': 1, 'b': 2} <class 'tuple'>

    keyword-only参数必须编写在**args任意关键字形式之前,且在*args任意位置形式之后(当二者都有的时候)。

    无论何时,一个参数名称出现在*args之前,它可能是默认位置参数,而不是keyword-only参数:

    def f(a,c=6,*b,**d): print(a,b,c,d)
    >>>f(1,2,3,x=4)
    1 (3,) 2  {'x':4}
  • 相关阅读:
    接口,抽象类,普通类
    将svn项目导出,再导入到其他工作空间
    Ajax 与 Struts 1
    save tran tranName
    hibernate缓存机制详细分析
    sql中的group by 和 having 用法解析
    TensorBoard 实践 1
    Tensorflow 解决MNIST问题的重构程序
    在MNIST数据集,实现多个功能的tensorflow程序
    Tensorflow中的滑动平均模型
  • 原文地址:https://www.cnblogs.com/wayneiscoming/p/7619729.html
Copyright © 2020-2023  润新知